gpt4 book ai didi

c - C中文件的结构内存分配,只有两个变量起作用

转载 作者:行者123 更新时间:2023-11-30 15:31:37 25 4
gpt4 key购买 nike

我必须编写一个程序,从文件中读取文本,将其分解为结构,验证各个部分是否符合特定标准,然后生成两个新文件;一份包含干净的数据,一份包含错误。到目前为止,我已经到了从文件中分解数据并将其存储到结构中的阶段,但它仅适用于前两个变量。文本由冒号分隔,我需要将每个部分放入下面的变量中文本文件示例

0001:0002:0003:0021:CLS

这是我的结构

struct packet{
int source;
int destination;
int type;
int port;
char data[50];
};

波纹管工作正常,但是一旦我添加另一个部分将数据添加到类型变量,程序就无法工作。

      fscanf(inFile, "%[^:]: %[^:]:", records[i].source, records[i].destination);
printf("%d - %s _ %s", i+1, records[i].source, records[i].destination);

但是这不起作用,但我需要它。好吧,我需要扩展它。

             fscanf(inFile, "%[^:]: %[^:]: %[^:]:", records[i].source, records[i].destination, records[i].type);
printf("%d - %s _ %s _ %s", i+1, records[i].source, records[i].destination, records[i].type);
}

如果我 printf 没有向结构输入任何内容,它会像我预期的那样显示 null,因为没有存储任何内容,所以我认为 fscanf 函数有问题。由于它适用于前两个,我不认为这是一个语法问题,所以它一定是一个内存问题。我已经使用了 malloc 和 realloc,但我对它感到困惑,并且我确信我没有做得正确。

完整代码列表

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

//declaration of function which will print my name and student number
const char * myname();
//declaration of a function that will prompt a user to enter a file and open it if it exists
int openfile();

struct packet{
int source;
int destination;
int type;
int port;
char data[50];
};


int main()
{
int recordCount = 0;

struct packet *records;
records =malloc(sizeof(struct packet));
// printing the my name and student number via the myname function
printf("%s\n", myname());
//executing the openfile function that will open a function
openfile(recordCount, records);


return 0;
}


const char * myname()
{
const char *x = "*************************\nUSERNAME\nUSER NUMBER\nCONTACT NUMBER\n*************************\n";
return x;
}


int openfile(int rCount, struct packet *records)
{
//file pointer which will keep track of the file being accessed
FILE *inFile ;
//creating variable that will hold what the user has entered for a filename to open
char inFileName[100] = { '\0'};

printf("Please Enter the File to open:");
//getting the users input and storing it into the variable just created
scanf("%s", inFileName);
//if the file does not exist, display an appropriate error message
if ((inFile = fopen(inFileName, "r")) == NULL)
{
printf("Cannot Open File **%s**\n", inFileName) ;
exit(1) ;
}

else {
//if the file does exist, process the data
while(fgets(inFileName, 100, inFile)!=NULL)
{


int i =0;
for (i=0; i<30;i++)
{
fscanf(inFile, "%[^:]: %[^:]: %[^:]:", records[i].source, records[i].destination, records[i].type);
printf("%d - %s _ %s _ %s", i+1, records[i].source, records[i].destination, records[i].type);
}



}

//close the file
fclose(inFile);
return 0;
}

};

最佳答案

你做错了:

fscanf(inFile, "%[^:]: %[^:]:", records[i].source, records[i].destination);

%[] 转换说明符用于字符串,但您将整数值传递为字符指针。未定义的行为!

您应该从任何现代编译器(即验证格式化字符串的编译器)中收到大量警告。

将整数解析为字符串是没有意义的,我不明白为什么你不这样做

fscanf(inFile, "%d:%d", &records[i].source, &records.destination);

对于第一种情况。

另外,请注意,使用 fgets() 读取整行更好,然后使用 sscanf() 解析读取的行 code>,而不是尝试将这两个步骤与 fscanf() 结合起来。

最后,您应该检查转换调用的返回值以了解有多少转换成功。

关于c - C中文件的结构内存分配,只有两个变量起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com