Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.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编程中fscanf()的问题_C++_C - Fatal编程技术网

C++ c编程中fscanf()的问题

C++ c编程中fscanf()的问题,c++,c,C++,C,我试图读取一个名为data的文件中的一些数据,该文件具有特定的格式。此文件中的数据为: 0 mpi_write() 100 1 mpi_write() 200 2 mpi_write() 300 4 mpi_write() 400 5 mpi_write() 1000 然后代码如下所示: #include<stdlib.h> #include<stdio.h> typedef struct tracetype{ int pid; char* opera

我试图读取一个名为data的文件中的一些数据,该文件具有特定的格式。此文件中的数据为:

0 mpi_write() 100
1 mpi_write() 200
2 mpi_write() 300
4 mpi_write() 400
5 mpi_write() 1000
然后代码如下所示:

#include<stdlib.h>
#include<stdio.h>

typedef struct tracetype{
    int pid;
    char* operation;
    int size;
}tracetyper;

void main(){
    FILE* file1;
    file1=fopen("./data","r");
    if(file1==NULL){
        printf("cannot open file");
        exit(1);
    }else{
        tracetyper* t=(tracetyper*)malloc(sizeof(tracetyper));
        while(feof(file1)!=EOF){
            fscanf(file1,"%d %s %d\n",&t->pid,t->operation,&t->size);

            printf("pid:%d,operation:%s,size:%d",t->pid,t->operation,t->size);
        }
        free(t);
    }
    fclose(file1);
}

使用gdb运行时,我发现fscanf不会将数据写入t->pid、t->operation和t->size。我的代码有什么问题吗?请帮帮我

您应该循环使用以下内容:

while ( (fscanf(file1,"%d %s %d\n",&t->pid,t->operation,&t->size)) != EOF) {
   printf("pid:%d,operation:%s,size:%d",t->pid,t->operation,t->size);
}
您还需要在结构中为char数组添加malloc。 此外,插入一张t as的支票


您应该循环使用以下内容:

while ( (fscanf(file1,"%d %s %d\n",&t->pid,t->operation,&t->size)) != EOF) {
   printf("pid:%d,operation:%s,size:%d",t->pid,t->operation,t->size);
}
您还需要在结构中为char数组添加malloc。 此外,插入一张t as的支票


您的程序有未定义的行为:您正在将%s数据读入未初始化的char*指针。您需要使用malloc分配操作,或者如果您知道最大长度(例如)为20个字符,则可以将该操作的固定字符串放入结构本身:

typedef struct tracetype{
    int pid;
    char operation[21]; // +1 for null terminator
    int size;
} tracetyper;
读取%s数据时,应始终告知fscanf长度限制,如下所示:

fscanf(file1,"%d %20s %d\n",&t->pid,t->operation,&t->size);
for (;;) { // Infinite loop
    ...
    if (fscanf(file1,"%d %20s %d",&t->pid,t->operation,&t->size) != 3) {
        break;
    }
    ...
}
最后,您应该删除字符串末尾的\n,并检查返回值的计数,而不是检查feof,如下所示:

fscanf(file1,"%d %20s %d\n",&t->pid,t->operation,&t->size);
for (;;) { // Infinite loop
    ...
    if (fscanf(file1,"%d %20s %d",&t->pid,t->operation,&t->size) != 3) {
        break;
    }
    ...
}

您的程序有未定义的行为:您正在将%s数据读入未初始化的char*指针。您需要使用malloc分配操作,或者如果您知道最大长度(例如)为20个字符,则可以将该操作的固定字符串放入结构本身:

typedef struct tracetype{
    int pid;
    char operation[21]; // +1 for null terminator
    int size;
} tracetyper;
读取%s数据时,应始终告知fscanf长度限制,如下所示:

fscanf(file1,"%d %20s %d\n",&t->pid,t->operation,&t->size);
for (;;) { // Infinite loop
    ...
    if (fscanf(file1,"%d %20s %d",&t->pid,t->operation,&t->size) != 3) {
        break;
    }
    ...
}
最后,您应该删除字符串末尾的\n,并检查返回值的计数,而不是检查feof,如下所示:

fscanf(file1,"%d %20s %d\n",&t->pid,t->operation,&t->size);
for (;;) { // Infinite loop
    ...
    if (fscanf(file1,"%d %20s %d",&t->pid,t->operation,&t->size) != 3) {
        break;
    }
    ...
}

另外,我发现在while循环中使用feof可能是不正确的,因为当运行这个程序时,它永远不会结束。还有什么意见吗?另外,我发现在while循环中使用feof可能不正确,因为当运行这个程序时,它永远不会结束。有什么意见吗?对“%20”保守一点也没有坏处。但是,如果字符串比预期的20长,那么您需要采取替代措施,至少扔掉多余的字符串。否则,下面的%d将得到错误的值,结果可能仍然是3。@LokiAstari是的,这只是最基本的设置,它假设一个合理控制的环境,即如果格式错误,可以修复文件。带有来自不受控环境的文件的实际生产代码将更加复杂。真的很有帮助。欣赏对“%20”保持保守并不有害。但是,如果字符串比预期的20长,那么您需要采取替代措施,至少扔掉多余的字符串。否则,下面的%d将得到错误的值,结果可能仍然是3。@LokiAstari是的,这只是最基本的设置,它假设一个合理控制的环境,即如果格式错误,可以修复文件。带有来自不受控环境的文件的实际生产代码将更加复杂。真的很有帮助。欣赏