[英]Strange symbols when reading text file with fgets
When trying to read a plain text file with fgets in C, i get some strange looking output on the first line. 当尝试用C中的fgets读取纯文本文件时,我在第一行得到一些奇怪的输出。 So if the first line is meant to be "hello" it comes out as something like "ELFh` 20120918 (prerelease)@xxhello".
因此,如果第一行是“hello”,它就像“ELFh` 20120918(预发行)@xxhello”。 Here is the code:
这是代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fr;
int i;
extern int uniq(char *previous_word, char *current_word);
char *line1 = malloc(500);
char *line2 = malloc(500);
char *temp;
for(i = 0; i<argc; i++)
{
fr = fopen (argv[i], "r");
while(fgets(line2, 499, fr) != NULL)
{
uniq(line1, line2);
temp = line1;
line1 = line2;
line2 = temp;
}
fclose(fr);
}
return 0;
}
int uniq(char *previous_word, char *current_word) {
if(!(current_word))
return 1;
if(strcmp(previous_word, current_word))
printf("%s", current_word);
return 0;
}
I've searched every description i can give of this problem on google and stack overflow and i can find nothing at all that fixes it. 我已经搜索了我可以在谷歌和堆栈溢出这个问题的每一个描述,我什么也都找不到修复它。
Your loop must begin at index 1. argv[0]
is your executable. 你的循环必须从索引1开始
argv[0]
是你的可执行文件。
To check argv[0]
is helpful if you have a so called multi binary executable. 如果你有一个所谓的多二进制可执行文件,检查
argv[0]
会很有帮助。 There you can handle different commands with just one binary. 只需一个二进制文件,您就可以处理不同的命令。 This is very helpful on embedded systems where you need to save memory.
这对于需要节省内存的嵌入式系统非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.