我练习用 C 语言处理文件。我需要读取一个记录了大约 9000000 个数据的文件。当我读取前 9000 条数据时它起作用了。但是,当我设置 NUM_DATA = 9000000 时,它总是崩溃。我该如何解决这个问题?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_LENGTH 10
#define NUM_DATA 9000000
int main(int argc, char **argv){
char **signal = malloc( NUM_DATA * MAX_LENGTH * sizeof(char *) );
char **svid = malloc( NUM_DATA * MAX_LENGTH * sizeof(char *) );
char GetString[110];
int num;
double tw[NUM_DATA], ip[NUM_DATA], qp[NUM_DATA]
char a[20], b[20], c[20], d[20], e[20], f[20], g[20], h[20], z[20], x[20], w[20];
FILE *fIn;
fIn = fopen(argv[1], "r");
// read …Run Code Online (Sandbox Code Playgroud) 浏览一些不属于我的旧代码,我遇到了这一行:
if (ungetc(getc(in), in) != EOF)
Run Code Online (Sandbox Code Playgroud)
in文件指针在哪里。
我不熟悉这些功能及其用途。
我是否正确地解释了这一点(尚未到达文件末尾)?
程序:
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
FILE *f;
char* line;
size_t ln=100;
char* s;
line=new char[100];
s=new char[100];
cout<<"input key"<<endl;
cin>>s;
f=fopen("parvin.txt","r");
if(f==NULL)
{
cout<<" no file TO read so creating for writing "<<endl;
//return 0;
f=fopen("parvin.txt","w");
fputs(s,f);
fputc('\n',f);
}
else
{
while(! feof(f))
{
fgets(line,100,f);
cout<<line<<endl;
//if(!strncmp(line,s,strlen(line)-1))
if(strcmp(line,s)== 0 )
{
cout<<"duplicate found"<<endl;
fclose(f);
return 0;
}
}
fclose(f);
f=fopen("parvin.txt","a+");
fputs(s,f);
fputc('\n',f);
}
fclose(f);
}
Run Code Online (Sandbox Code Playgroud)
这里是我喜欢读取输入字符串并将其写入文件的上述程序,前提是字符串不存在于文件中.
在读取模式下打开文件.
如果第一次输入文件不存在,如果文件指针返回NULL,则创建一个文件写入模式并写入输入的字符串.
可能重复:
避免在printf()中尾随零
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *file;
double n;
file = fopen("fp.source", "r");
while(!feof(file)) {
fscanf(file, "%lf", &n);
printf("Next double:\"%lf\"\n", n);
}
fclose(file);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
嗨,我正在尝试扫描浮点数,我已经让它工作,但我得到了我不想要的尾随零.有办法避免这种情况吗?例如,我得到的当前输出是:下一个双倍:"11.540000"
实际上我想:下一个双:"11.54"
我正在尝试在C中减少一个FILE指针,但是我没有完成它,这是我的代码:
#include <math.h>
#include <stdio.h>
#include <complex.h>
int main() {
FILE* inputFile = NULL;
double* inputData = NULL;
unsigned int windowSize = 512;
unsigned int index1 = 0, index2 = 0, i = 0;
double temp = 0.0;
// mememory allocation
inputData = (double*) malloc(sizeof(double)*windowSize);
// Opning files
inputFile = fopen(" file","rb");
if (inputFile == NULL) {
printf("Couldn't open either the input or the output file \n");
return -1;
}
while((i=fread(inputData,sizeof(double),windowSize,inputFile))==windowSize){
for (index1 =0; index1 < windowSize;index1++) {
printf("index %d …Run Code Online (Sandbox Code Playgroud) 我正在开发一个项目,我在一个单独的函数内创建结构main().在创建并将成员(变量)信息添加到结构变量中之后,我需要能够return指向结构的指针,我可以在其中访问它以供参考和打印.
我包括我的代码,请尽可能简单,因为我是结构新手.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct _car
{
char color[20];
char model[20];
char brand[20];
int year;
struct _car *eptr;
} Car;
Car* readCars(char* filename);
/*
*
*/
int main(int argc, char** argv) {
Car *Cars;
Car *eptr;
Cars = readCars(argv[1]);
printf("%s%s%s%s", Cars->color, Cars->brand, Cars->model, Cars->color);
return (EXIT_SUCCESS);
}
Car* readCars(char* filename)
{
FILE *file = fopen(filename, "r");
int year;
char color [20], model [20], brandx[20];
Car car1;
Car car2;
Car *carptr;
Car *car2ptr; …Run Code Online (Sandbox Code Playgroud) 伙计们,我想从我的文件中读取文本,并将每个字符分配给数组的单个元素
char A[1000];
FILE * fpointer;
fpointer=fopen("text.txt","r");
i=0;
while(!feof(fpointer))
{
fscanf(fpointer,"%c",&A[i]);
i=i+1;
}
fclose(fpointer);
for (i=0;i<100;i++)
{
printf("%c",A[i]);
}
return 0;
Run Code Online (Sandbox Code Playgroud)
但问题是输出是一些奇怪的符号而不是文件的文本,这是"这只是一个测试".为什么会发生这种情况?
我知道在C++中,EOF字符会自动附加到文件末尾,例如filestream.close.我想知道,这是在C中也是默认完成的吗?
我已经看到了一些从stackoverflow上的函数返回数组的例子.我按照这些例子,但我仍然收到警告.
#include <stdio.h>
#include <stdlib.h>
char * getNum();
int main(){
int * num;
int * i;
num = getNum();
puts(num);
return 0;
}
char * getNum(){
FILE * fp;
fp = fopen("number", "r"); //getting a 1000 digit number from a file
char * n; //putting it in "array"
n = (char *)malloc(1000 * sizeof(char));
char x[100];
int i, j = 0;
while(!feof(fp)){
fgets(x, 100, fp);
for(i=0; i<50; i++){ //getting the first 50 characters in a line
n[j] = x[i]; //to …Run Code Online (Sandbox Code Playgroud) 在读/写操作期间是否绝对有必要检查feof()?
我问的原因是因为我有一个执行读/写一次的程序,下面是代码:
while (1) {
data = read_file_data(file);
write_file_data(data, filename);
if (feof(file))
print(read error)
}
Run Code Online (Sandbox Code Playgroud)
这只是伪代码,但是是否有必要检查feof()像这样一次读取的情况?目前,我认为只有当您在上面的阅读之后再阅读一遍时才有必要,如下所示:
while (1) {
data = read_file_data(file);
write_file_data(data, filename);
if (feof(file)) // feof error occurred oops
print(read error)
data = read_file_data(file); // reading after error
}
Run Code Online (Sandbox Code Playgroud)
EOF reached最后,即使发生错误(读过去) ,阅读会产生什么后果EOF?
在一本书中,我看到了一段代码.
但当我运行此代码时,它说:C:\ Users\dipankar\Desktop\cla.cpp [错误]'延迟'未在此范围内声明
他们没有适当的文档就使用它.他们只说"delay()用于延迟下一行的执行几毫秒"!! 请帮忙.