如何让 C 程序向文件内容添加行号。例如,“1. #include <stdio.h> 2. #include <stdlib.h>”等等?
How can you make a C program add line numbers to a file's contents. E.g., "1. #include <stdio.h> 2. #include <stdlib.h>", etc.?
#include <stdio.h>
#include <stdlib.h>
int process_stream(FILE *fpntr);
char *fgetline(FILE *fpntr);
int main(int argc, char* argv[]) {
FILE *fpntr;
char filename[100], c;
int a = 0;
printf("Please enter a file name/directory: \n");
scanf("%s", filename);
fpntr = fopen(filename, "r");
if (fpntr == NULL) {
printf("cannot open file \n");
exit(0);
}
//read contents from file
c = fgetc(fpntr);
while (c != EOF){
printf ("%c", c);
c = fgetc(fpntr);
if (c == '\n')
{
a++;
printf("%d", a);
}}
fclose(fpntr);
return 0;
exit (0);
}
首先按照你说的去做
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE *fptr;
char filename[100];
int c;
int lineNumber = 1;
printf("Please enter a file name/directory: \n");
scanf("%99s", filename);
// Open the file
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("cannot open file \n");
exit(0);
}
//read contents from file
// While ((c = read a character) is not EOF)
while ((c = fgetc(fptr)) != EOF){
// If (c is \n)
if (c == '\n') {
// Print "lineNumber", then increment it
printf("%d. ", lineNumber);
lineNumber++;
}
// Print c
printf ("%c", c);
// End while
}
// Close the file
fclose(fptr);
return 0;
}
不幸的是,此代码无法正常运行,其输出为
#include <stdio.h>1.
#include <stdlib.h>2.
int hoge;3.
int fuga;4.
当输入文件是
#include <stdio.h>
#include <stdlib.h>
int hoge;
int fuga;
错误是您在行尾打印行号。
它们应该在行的开头。
我会使用一个标志来指示行的开头。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE *fptr;
char filename[100];
int c;
int lineNumber = 1;
int isBeginningOfLine = 1;
printf("Please enter a file name/directory: \n");
scanf("%99s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("cannot open file \n");
exit(0);
}
//read contents from file
while ((c = fgetc(fptr)) != EOF){
if (isBeginningOfLine) {
print("%d. ", lineNumber);
isBeginningOfLine = 0;
}
if (c == '\n') {
lineNumber++;
isBeginningOfLine = 1;
}
printf ("%c", c);
}
fclose(fptr);
return 0;
}
现在我得到了
1. #include <stdio.h>
2. #include <stdlib.h>
3. int hoge;
4. int fuga;
#include <stdio.h>
#include <stdlib.h>
int process_stream(FILE *fpntr);
char *fgetline(FILE *fpntr);
int main(int argc, char* argv[]) {
FILE *fpntr;
char filename[100], c;
int a = 0;
printf("Please enter a file name/directory: \n");
scanf("%s", filename);
fpntr = fopen(filename, "r");
if (fpntr == NULL) {
printf("cannot open file \n");
exit(0);
}
//read contents from file
c = fgetc(fpntr);
while (c != EOF){
printf ("%c", c);
c = fgetc(fpntr);
if (c == '\n')
{
a++;
printf("%d", a);
}}
fclose(fpntr);
return 0;
exit (0);
}
首先按照你说的去做
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE *fptr;
char filename[100];
int c;
int lineNumber = 1;
printf("Please enter a file name/directory: \n");
scanf("%99s", filename);
// Open the file
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("cannot open file \n");
exit(0);
}
//read contents from file
// While ((c = read a character) is not EOF)
while ((c = fgetc(fptr)) != EOF){
// If (c is \n)
if (c == '\n') {
// Print "lineNumber", then increment it
printf("%d. ", lineNumber);
lineNumber++;
}
// Print c
printf ("%c", c);
// End while
}
// Close the file
fclose(fptr);
return 0;
}
不幸的是,此代码无法正常运行,其输出为
#include <stdio.h>1.
#include <stdlib.h>2.
int hoge;3.
int fuga;4.
当输入文件是
#include <stdio.h>
#include <stdlib.h>
int hoge;
int fuga;
错误是您在行尾打印行号。 它们应该在行的开头。 我会使用一个标志来指示行的开头。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE *fptr;
char filename[100];
int c;
int lineNumber = 1;
int isBeginningOfLine = 1;
printf("Please enter a file name/directory: \n");
scanf("%99s", filename);
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("cannot open file \n");
exit(0);
}
//read contents from file
while ((c = fgetc(fptr)) != EOF){
if (isBeginningOfLine) {
print("%d. ", lineNumber);
isBeginningOfLine = 0;
}
if (c == '\n') {
lineNumber++;
isBeginningOfLine = 1;
}
printf ("%c", c);
}
fclose(fptr);
return 0;
}
现在我得到了
1. #include <stdio.h>
2. #include <stdlib.h>
3. int hoge;
4. int fuga;