如何从文件中读取并解析
我有一个文件.txt包含一些格式的值:
0,30,25,10
现在,我打开文件并将其存储到一个数组中
char imposta_tratt[300];
FILE *fp;
fp = fopen("/home/pi/Documents/imposta_trattamento.txt", "r");
if (fp == 0) return;
fread(imposta_tratt, sizeof(imposta_tratt), 1, fp);
fclose(fp);
,现在我希望将数组填充有我的数据。我的值由,
隔开,因此我继续对其进行解析:
const char delim[2] = ",";
int t=0;
char *token = strtok(imposta_tratt, delim);
while (token!=NULL){
strcpy(tratt[t],token);
token = strtok(NULL, delim);
tratt[t]=token;
t++;
}
在这里,参考文件.txt中的内容,我希望有tratt [0] = 0; Tratt [1] = 30; tratt [2] = 25;
等等,但似乎我缺少某些东西,因为它不是这样。
我想要的是将存储在单个变量中的TXT文件的值。有人可以帮忙吗?
I have a file .txt containing some values formatted like this:
0,30,25,10
Now, I open up the file and store it into an array
char imposta_tratt[300];
FILE *fp;
fp = fopen("/home/pi/Documents/imposta_trattamento.txt", "r");
if (fp == 0) return;
fread(imposta_tratt, sizeof(imposta_tratt), 1, fp);
fclose(fp);
Now I expect to have the array filled with my data. I have the values separated by a ,
so I go on and parse it:
const char delim[2] = ",";
int t=0;
char *token = strtok(imposta_tratt, delim);
while (token!=NULL){
strcpy(tratt[t],token);
token = strtok(NULL, delim);
tratt[t]=token;
t++;
}
Here, referring to what's in the file .txt, I expect to have tratt[0]=0; tratt[1]=30; tratt[2]=25;
and so on, but seems like I am missing something since it's not like this.
All I want is to have the values of the txt file stored in single variables. Can someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您要实现的目标可以简单地使用
fgets()
:const char *fileName
:您要解析的文件。const size_t tsizemax
:tratt
数组的最大大小。控制大小很重要,否则您的代码将具有缓冲区溢出(例如,例如,您的文件何时具有100多个令牌)。int tratt [tsizemax]
:将保持值的数组。size_t *tsize
:代币的读数(用于tsizemax
的组合)。const char *delim
:在您的情况下,定界符a,
。这是您的
main()
:输出:
What you are trying to achieve can simply be done using
fgets()
:const char *filename
: The file you want to parse.const size_t tsizemax
: The maximum size of yourtratt
array. It is important to control the size, otherwise your code will have buffer overflow (think of when your file has more than 100 tokens, for example).int tratt[tsizemax]
: The array that will hold the values.size_t *tsize
: The number of tokens read (used in combination oftsizemax
).const char *delim
: The delimiter(s), in your case a,
.This is your
main()
:Output:
假设
“ in.txt”
具有以下程序使用
fscanf
将整数读取到tratt
阵列中,一对一。当我们使用fscanf
读取整数时,我们确保其返回值如预期。如果没有,我们关闭文件并退出。如果fscanf
的返回值不如预期,则该程序还会打印出哪种错误类型。当前,如果发生任何错误,程序将停止。但是,您可以根据愿意的错误,使程序行为不同。随着输出,程序将所有整数读取到
tratt
数组中。 现在,输出是该程序假设我们知道要阅读到
tratt
的元素数量。如果不这样记录有关该文件的信息,例如文件中的数字数量和数据类型(最适合二进制文件)。这些只是几种可能性。一种更好的方法可能是读取字符(例如,使用
getc
),然后使用strtol
将字符数字序列转换为long int
(我会采取类似的方法)。然而,这种方法更简洁,应该足够。
Suppose
"in.txt"
has contentsThe below program uses
fscanf
to read the integers into thetratt
array, one-by-one. As we read integers usingfscanf
, we make sure it's return value is as expected. If not, we close the file and exit. In the event that the return value offscanf
is not as expected, the program also prints which type of error occurred. Currently, if any error occurs, the program stops. However, you can make the program behave differently depending on the error that occurred if you like.As output, the program prints all of the integers read into the
tratt
array. The output isNow this program assumes we know the number of elements we want to read into
tratt
. If we do not, we could allow for dynamically allocating more memory should the array need more elements or perhaps"in.txt"
could contain a data structure, say, at the beginning/end of the file that records information about the file, such as the number of numbers in the file and the data type (a binary file would be best suited for this). These are just a couple of the possibilities.A better approach might be to read characters in one-by-one (say, using
getc
) and usestrtol
to convert a sequence of character digits to along int
(I would have taken an approach similar to this).Nevertheless, this approach is more succinct and should suffice.