使用fscanf在C中逐行读取文本文件
Reading text file line by line in C using fscanf
我试图让这个程序从一个文本文件中读取并逐行写入另一个文本文件。我让它读取文件并写入文件,但它只执行最后一行。我已经用谷歌搜索了好几天,并尝试了几种不同的建议方法,但这是我得到的最接近的方法。我觉得我很亲近。我还把它打印到屏幕上以帮助调试。
到目前为止,这是我的代码:
#include <stdlib.h>
#include <unistd.h>
#define MAX 20
int main()
{
char fileInName[MAX];
char fileOutName[MAX];
int empNum = 0;
char givenName[MAX];
char surname[MAX];
char dept[MAX];
float ytd;
float payRate;
float hours;
printf("Enter the name of the input file (Maximum of 15 characters): ");
scanf("%s", fileInName);
FILE *inFile = fopen(fileInName, "r");
if(access(fileInName, F_OK) == -1)
{
printf("Input file does not exist! Program terminating.");
exit(0);
}
printf("Enter the name of the file to hold the results (Maximum of 20 characters): ");
scanf("%s", fileOutName);
FILE *outFile = fopen(fileOutName, "w");
while(fscanf(inFile, "%d\t%s\t%s\t%s\t%f\t%f\t%f\n", &empNum, givenName,
surname, dept, &ytd, &payRate, &hours) != EOF);
{
printf("%d\t%s %s\t%s\t$%.2f\t$%.2f\t%.2f\n",
empNum, givenName, surname, dept, ytd, payRate, hours);
fprintf(outFile, "%d\t", empNum);
fprintf(outFile, "%s\t", givenName);
fprintf(outFile, "%s\t", surname);
fprintf(outFile, "%s\t", dept);
fprintf(outFile, "$%.2f\t", ytd);
fprintf(outFile, "$%.2f\t", payRate);
fprintf(outFile, "%.2f\n", hours);
}
fclose(inFile);
fclose(outFile);
return 0;
}
输入:
6 Ab-Karim Khasby Acct 110100.00 13.24 40.0
10 Castillo Jorge Acct 66600.00 17.87 64.5
22 Cofer Matthew Acct 36600.00 10.00 10.0
24 Davidson Jacory Acct 110090.00 41.3 36.7
13 Foley Zachery Acct 1358.34 16.22 53.51
5 Gonzalez Eduardo DP 110090.00 10.00 1.0
3 Gutierrez Thomas Mgt 673478.34 187.56 40.0
1 Holder David Mgt 134234.34 67.42 56.25
4 Johnsen Samuel Sales 11345.22 23.77 67.3
12 Koirala Akriti Sales 234.56 17.56 38.9
17 Lasater William Sales 2342.34 27.86 45.6
7 Martin Colton Sales 67000.00 34.23 40.0
86 Perkins Felix Inven 12345.78 13.24 40.0
66 Reasons Joshua Inven 109998.75 57.87 64.5
51 Schultz Jesse Inven 206600.00 10.00 10.0
38 Stevens Marissa Inven 78342.00 51.3 36.75
预期输出在新文本文件中看起来几乎相同。
当前输出:
38 Stevens Marissa Inven 342.00 .30 36.75
行
while(fscanf(inFile, "%d\t%s\t%s\t%s\t%f\t%f\t%f\n", &empNum, givenName,
surname, dept, &ytd, &payRate, &hours) != EOF);
由于末尾的 ;
创建了一个空循环。该循环读取所有数据,然后输出最后一组读取的数据。
你应该使用
while(fscanf(inFile, "%d\t%s\t%s\t%s\t%f\t%f\t%f\n", &empNum, givenName,
surname, dept, &ytd, &payRate, &hours) != EOF)
我试图让这个程序从一个文本文件中读取并逐行写入另一个文本文件。我让它读取文件并写入文件,但它只执行最后一行。我已经用谷歌搜索了好几天,并尝试了几种不同的建议方法,但这是我得到的最接近的方法。我觉得我很亲近。我还把它打印到屏幕上以帮助调试。
到目前为止,这是我的代码:
#include <stdlib.h>
#include <unistd.h>
#define MAX 20
int main()
{
char fileInName[MAX];
char fileOutName[MAX];
int empNum = 0;
char givenName[MAX];
char surname[MAX];
char dept[MAX];
float ytd;
float payRate;
float hours;
printf("Enter the name of the input file (Maximum of 15 characters): ");
scanf("%s", fileInName);
FILE *inFile = fopen(fileInName, "r");
if(access(fileInName, F_OK) == -1)
{
printf("Input file does not exist! Program terminating.");
exit(0);
}
printf("Enter the name of the file to hold the results (Maximum of 20 characters): ");
scanf("%s", fileOutName);
FILE *outFile = fopen(fileOutName, "w");
while(fscanf(inFile, "%d\t%s\t%s\t%s\t%f\t%f\t%f\n", &empNum, givenName,
surname, dept, &ytd, &payRate, &hours) != EOF);
{
printf("%d\t%s %s\t%s\t$%.2f\t$%.2f\t%.2f\n",
empNum, givenName, surname, dept, ytd, payRate, hours);
fprintf(outFile, "%d\t", empNum);
fprintf(outFile, "%s\t", givenName);
fprintf(outFile, "%s\t", surname);
fprintf(outFile, "%s\t", dept);
fprintf(outFile, "$%.2f\t", ytd);
fprintf(outFile, "$%.2f\t", payRate);
fprintf(outFile, "%.2f\n", hours);
}
fclose(inFile);
fclose(outFile);
return 0;
}
输入:
6 Ab-Karim Khasby Acct 110100.00 13.24 40.0
10 Castillo Jorge Acct 66600.00 17.87 64.5
22 Cofer Matthew Acct 36600.00 10.00 10.0
24 Davidson Jacory Acct 110090.00 41.3 36.7
13 Foley Zachery Acct 1358.34 16.22 53.51
5 Gonzalez Eduardo DP 110090.00 10.00 1.0
3 Gutierrez Thomas Mgt 673478.34 187.56 40.0
1 Holder David Mgt 134234.34 67.42 56.25
4 Johnsen Samuel Sales 11345.22 23.77 67.3
12 Koirala Akriti Sales 234.56 17.56 38.9
17 Lasater William Sales 2342.34 27.86 45.6
7 Martin Colton Sales 67000.00 34.23 40.0
86 Perkins Felix Inven 12345.78 13.24 40.0
66 Reasons Joshua Inven 109998.75 57.87 64.5
51 Schultz Jesse Inven 206600.00 10.00 10.0
38 Stevens Marissa Inven 78342.00 51.3 36.75
预期输出在新文本文件中看起来几乎相同。
当前输出:
38 Stevens Marissa Inven 342.00 .30 36.75
行
while(fscanf(inFile, "%d\t%s\t%s\t%s\t%f\t%f\t%f\n", &empNum, givenName,
surname, dept, &ytd, &payRate, &hours) != EOF);
由于末尾的 ;
创建了一个空循环。该循环读取所有数据,然后输出最后一组读取的数据。
你应该使用
while(fscanf(inFile, "%d\t%s\t%s\t%s\t%f\t%f\t%f\n", &empNum, givenName,
surname, dept, &ytd, &payRate, &hours) != EOF)