我是新来的论坛和c一般,所以请耐心等待我。我正在尝试编写一个带有文本文件并解析所有单词和字符的c程序,然后将它们保存到输出文本文件中。我正在使用C99,Windows 7-64bit,MinGW,记事本,记事本++和txt文件的ASNI格式。我已经读过fgets()比fscanf用于读取输入更好,因为它有缓冲区溢出保护,所以我决定尝试使用它,但它在测试文件中有一些标点符号的问题(我认为这是回车\ r)。我尝试使用fscanf,除了它跳过所有的空白(我可以在以后添加,不关心它),它似乎接受所有的文本,并在输出文件中打印它。
这是我的测试代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void main(int argc, char* argv[])
{
int limit=100, flimit=0, flimitmax=1900000000; //I stopped flimitmax short of the 2GB mark
char name[limit], copyname[limit];
FILE *data, *output;
//Gets the value of a specified data file for reading
printf("\nPlease specify a file to format for data input. NOTE: CHARACTERS LIMITIED TO %d\n", limit);
fgets(name, limit, stdin); //takes input of size 'limit' and assigns it to 'name'
size_t ln = strlen(name); //gets the size of name[]
if(name[ln-1]=='\n') name[ln-1]='\0'; //gets rid of newline character read in by fget
strncpy(copyname, name, limit); //stores the value of the specified file name for use making the input and output files
strcat(name, ".txt"); //appends .txt file extension to the file name
printf("\nYou chose file %s\n", name);
data = fopen(name, "r"); //Checks to see if the specified data file exists and if it can be read
if(data==NULL)
{
fprintf(stderr, "\nCan't open file %s!!!\n", name);
exit(1);
}
//Gets the size of the data file being worked. Used later when the file is copied into the program using fgets.
fseek(data, 0, SEEK_END); // seek to end of file
flimit = ftell(data)+1; // get current file pointer
fseek(data, 0, SEEK_SET); // seek back to beginning of file
if((flimit > flimitmax) || (flimit < 0))//Checks to see if flimit falls between 0 and 1.9GB. If not, the file is larger than 1.9GB
{
printf("Error, max file size exceeded. Program terminating\n");
exit(1);
}
printf("File size is %d bytes\n", flimit);
//Creates a name for the output file
strncpy(name, copyname, limit); //reassigns original value to name to make output file name
strcat(name, "OUT.txt"); //appends OUT.txt file extension to the file name
printf("\nOutput file is %s\n", name);
output = fopen(name, "w"); //checks to see if the Input file exists and if it can be read
if(output==NULL)
{
fprintf(stderr, "\nCan't open file %s!!!\n", name);
exit(1);
}
//Reads the data file and assigns values to the input and output files
char filein[flimit]; //I created this variable here to avoid issues of array resizing.
//fgets attempt
fgets(filein, flimit, data); //scans the whole datafile and stores it in the char array.
printf("\n%s\n", filein);
fprintf(output, filein);
memset(&filein[0], 0, sizeof(filein)); //clears the filein array
fseek(data, 0, SEEK_SET); // seek back to beginning of file
//fscanf attempt
while(fscanf(data, "%s", &filein)!=EOF)
{
printf("\n%s\n", filein);
fprintf(output, filein);
}
//Closes the files and ends the program
printf("\nDONE!!!\n");
fclose(data);
fclose(output);
}
以下是我在数据文件中使用的文字:
Things/Words and punctuation: The Test
This is a test (mostly to see if this program is working).
这是我从输出文件中得到的输出:
Things/Words and punctuation: The Test
Things/Wordsandpunctuation:TheTestThisisatest(mostlytoseeifthisprogramisworking).
为什么fgets()会挂断?它得到第一行就好了,然后就会卡住。
提前感谢您花时间看这个。如果您对我的代码有任何其他建议,请随时告诉我。
答案 0 :(得分:0)
“当读取flimit字符时,fgets函数将停止读取,在filein中遇到第一个换行符,或者在文件结尾处,以先到者为准。”
你的fgets()遇到换行符并停止。这就是为什么你需要使用while循环来保持代码运行直到你到达文件的末尾。成功完成后,fgets()返回一个流。如果此流位于文件结尾,则fgets()返回空指针。
以下代码块解决了您的问题,
...
while(fgets(filein,flimit,data) != NULL)
{
printf("%s\n",filein);
fprintf(output,filein);
}
...
以下链接通过示例说明了fgets功能的正确用法。
参考:http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html
希望它有所帮助,