Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 从文件中读取多行数据_C_Loops_Malloc - Fatal编程技术网

C 从文件中读取多行数据

C 从文件中读取多行数据,c,loops,malloc,C,Loops,Malloc,我从预读文件中读取数据,并将其存储在缓冲区中,在缓冲区中,我已通过一个结构来组织数据,然后将其重新保存到另一个文件中 然而,我只阅读了一行代码 我的代码 -打开文件: File *p_file char fileLocation[40]; char buff[1000]; printf("\nEnter file name: \n"); scanf("%s, fileLocation); p_file = fopen(fileLocation, "r"); if(!p_file) {

我从预读文件中读取数据,并将其存储在缓冲区中,在缓冲区中,我已通过一个结构来组织数据,然后将其重新保存到另一个文件中

然而,我只阅读了一行代码

我的代码 -打开文件:

File *p_file

char fileLocation[40];
char buff[1000];

printf("\nEnter file name: \n");
scanf("%s, fileLocation);

p_file = fopen(fileLocation, "r");

if(!p_file)
{
    printf("\nError!\n");
}
循环读取数据并保存文件

while(fgets(buff, 1000, p_file)!=NULL
{
    printf("%s", buff);

    storedData.source = atoi(strtok(buff, ":");
    storedData.destination = atoi(strtok(0, ":");
    storedData.type = atoi(strtok(0, ":");
    storedData.port = atoi(strtok(0, ":");
    storedData.data = strtok(0, ":\n");

    printf("\nEnter a File Name to save:");
    scanf("%s", fileLocation);

 if ((p_file = fopen(fileLocation, "w")) == NULL
 {
    puts("\n Could not point to the file.");
 }else{
    printf("\nSaving");
    fprintf(p_file, "%04d:%04d:%04d:%04:%s \n",
            storedData.source,
            storedData.destination,
            storedData.type,
            storedData.port,
            storedData.data );

    fclose(p_file);
 }
fclose(p_file);
数据的当前输出:

0001:0002:0003:0021:CLS
0001:0002:0003:0021:CLS
0001:0010:0003:0021:CLS
0001:0002:0002:0080:<HTML>
想要的数据输出:

0001:0002:0003:0021:CLS
0001:0002:0003:0021:CLS
0001:0010:0003:0021:CLS
0001:0002:0002:0080:<HTML>
0001:0002:0003:0021:CLS
0001:0010:0003:0021:CLS
0001:0002:0002:0080:

我相信我必须声明一个整数值,用于循环文件内容,以及使用malloc获取结构的大小,但我不知道如何执行此操作。任何帮助都将不胜感激

您过度使用
p_文件
来读取和写入文件

if ((p_file = fopen(fileLocation, "w")) == NULL)
这样,您就失去了为读取而打开的文件的指针。 当你在
else
part
fgets()
中关闭它时,你会认为没有更多的行了

使用其他变量来写入文件

if ((p_file = fopen(fileLocation, "w")) == NULL)


如果要处理缓冲数据,请将
while(fgets()…
更改为读取所有行,然后处理每行。
fgets()
不会读取多行。

您的循环实际上只执行一次操作

storedData.data=strtok(0,:\n”);

所以你只需要第一行。

这一行:while(fgets(buff,1000,p_file)!=NULL缺少一个尾随’’这一行:如果((p_file=fopen(fileLocation,“w”)==NULL缺少尾随’’,同样,为文件指针使用不同的名称,否则1)将无法进一步引用第一个文件,2)无法关闭第一个文件3)只需打开一次输出文件,因此请将其放置在loo之外;
FILE *in_file;
FILE *out_file;
char fileLocation[40];
char buff[1000];

printf("\nEnter file name: \n");
if( 1 != scanf(" %s, fileLocation) )
{
    perror( " scanf failed for input file: );
    exit( EXIT_FAILURE );
}

if( NULL == (in_file = fopen(fileLocation, "r") )
{
    perror( "fopen failed for input file" );
    exit( EXIT_FAILURE );
}

printf("\nEnter a File Name to save:");
if( 1 != scanf(" %s", fileLocation) )
{
    perror( "scanf failed for outut file name" );
    fclose( in_file ); // cleanup
    exit( EXIT_FAILURE );
}

if ( NULL == (out_file = fopen(fileLocation, "w")) )
{
    perror( " fopen failed for output file" );
    fclose( in_file ); // cleanup
    exit( EXIT_FAILURE )l
}

while(fgets(buff, 1000, p_file)!=NULL) )
{
    printf("%s", buff);

    storedData.source = atoi(strtok(buff, ":");
    storedData.destination = atoi(strtok(0, ":");
    storedData.type = atoi(strtok(0, ":");
    storedData.port = atoi(strtok(0, ":");
    storedData.data = strtok(0, ":\n");

    printf("\nSaving");
    fprintf(p_file, "%04d:%04d:%04d:%04:%s \n",
        storedData.source,
        storedData.destination,
        storedData.type,
        storedData.port,
        storedData.data );
} // end while

fclose( in_file );  // cleanup
fclose( out_file ); // cleanup