这是我目前拥有的代码,但似乎无法编译。任何帮助将非常感激。
#include <stdio.h>
main (void)
{
FILE *textfile1;
FILE *textfile2;
int c;
char Filename;
printf("Enter the name of text file:");
scanf("%s", &Filename;
textfile1 = fopen(textfile1, "r");
if (textfile1 == NULL)
{
printf("File not found");
}
else
{
while (fscanf(textfile1, "%c", &textfile1) == 1)
{
printf("%c", textfile1);
}
fclose(textfile1);
}
}
最佳答案
#include <stdio.h>
int main (void) /* you had better specify the return value explicitly */
{
FILE *textfile1;
FILE *textfile2;
int c;
char Filename[1024]; /* allocate enough buffer to store the name */
char textfiledata1; /* add this to store the data from file */
printf("Enter the name of text file:");
scanf("%1023s", Filename); /* add ), and other change including limit of length to read to avoid buffer overrun */
textfile1 = fopen(Filename, "r"); /* you have to pass the name. Do not pass the uninitialized file pointer! */
if (textfile1 == NULL)
{
printf("File not found");
}
else
{
while (fscanf(textfile1, "%c", &textfiledata1) == 1) /* store the data from file to the file pointer? nonsense. */
{
printf("%c", textfiledata1);
}
fclose(textfile1);
}
return 0; /* explicitly return something is a good practice */
}
关于c - 我需要创建一个程序,提示用户输入要读取的两个输入文件的名称。然后读取文件并显示统计信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34046259/