[英]How to read data from a text file and store it in a variable in C language?
I am trying to read a text file in C.我正在尝试读取 C 中的文本文件。 The name of the file is test.txt and has the following kind of format.文件的名称是test.txt ,格式如下。
Nx = 2
Ny = 4
T = 10
I have written this C code to read the values of Nx, Ny, and T which is 2, 4, and 10 respectively.我编写了这个 C 代码来读取 Nx、Ny 和 T 的值,它们分别为 2、4 和 10。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
double Data[3]; // I'm interested in this information
char junk1, junk2; // junk variables to avoid first two characters
FILE * file = fopen("test.txt", "r"); // open file
for(int i = 0; i < 3; i++) // each loop will read new line of file; i<3 for 3 lines in file
{
fscanf(file, "%s %s %lf\n", &junk1, &junk2, &Data[i]); //store info in Data array
printf("%f\n", Data[i]); // print Data, just to check
}
fclose(file);
int Nx; // store data in respective variables
int Ny;
double T;
Nx = Data[0];
Ny = Data[1];
T = Data[2];
printf("Value of Nx is %d\n", Nx); // Print values to check
printf("Value of Ny is %d\n", Ny);
printf("Value of T is %f\n", T);
}
But got this as an output.但是得到了这个 output。 This output is wrong as the values of Nx, Ny, and T are not matching with the data given above.这个 output 是错误的,因为 Nx、Ny 和 T 的值与上面给出的数据不匹配。
Please help me to solve this problem.请帮我解决这个问题。
junk1
and junk2
should be arrays of char to be able to store strings. junk1
和junk2
应该是 char 的 arrays 才能存储字符串。
But since it is junk you could simply not store it anywhere by using *
in the fscanf
conversion specifiers:但是由于它是垃圾文件,因此您不能通过在fscanf
转换说明符中使用*
将其存储在任何地方:
fscanf(file, "%*s %*s %lf\n", &Data[i]);
fscanf
documentation: https://en.cppreference.com/w/c/io/fscanf fscanf
文档: https://en.cppreference.com/w/c/io/fscanf
Your program makes strong assumptions about the input file:您的程序对输入文件做出了强有力的假设:
"test.txt"
exists in the current directory and can be read文件"test.txt"
存在于当前目录,可以读取Nx
, Ny
, T
.它包含至少 3 个设置,顺序为Nx
、 Ny
、 T
。It has problems too:它也有问题:
%s
into a single character variable junk1
will cause undefined behavior, same for junk2
, because fscanf()
will attempt to store all characters from the string plus a null terminator at the destination address, overwriting other data with potentially catastrophic consequences.将带有%s
的字符串读入单个字符变量junk1
将导致未定义的行为,对于junk2
,因为fscanf()
将尝试存储字符串中的所有字符以及目标地址处的 null 终止符,从而覆盖其他可能造成灾难性后果的数据.main
has a return type int
. main
有一个返回类型int
。Here is a more generic approach:这是一个更通用的方法:
#include <stdio.h>
#include <string.h>
int main() {
int Nx = 0, Ny = 0;
double T = 0;
int has_Nx = 0, has_Ny = 0, has_T = 0;
char buf[80];
FILE *file;
if ((file = fopen("test.txt", "r")) == NULL) {
fprintf(stderr, "cannot open test.txt\n");
return 1;
}
while (fgets(buf, sizeof buf, file)) {
if (buf[strspn(buf, " ")] == '\n') /* accept blank lines */
continue;
if (sscanf(buf, " Nx = %d", &Nx) == 1)
has_Nx = 1;
else
if (sscanf(buf, " Ny = %d", &Ny) == 1)
has_Ny = 1;
else
if (sscanf(buf, " T = %lf", &T) == 1)
has_T = 1;
else
fprintf(stderr, "invalid line: %s", buf);
}
fclose(file);
// Print values to check
if (has_Nx)
printf("Value of Nx is %d\n", Nx);
if (has_Ny)
printf("Value of Ny is %d\n", Ny);
if (has_T)
printf("Value of T is %g\n", T);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.