简体   繁体   English

C程序从.txt文件中获取每行的第一个单词,并将该单词打印到另一个.txt文件中:某种工作方式还可以打印随机字母

[英]C program to get first word of each line from a .txt file and print that word onto another .txt file: Kind of works but also prints random letters

So we have this file called dictionary1.txt and it has words with their pronounciation right next to them. 因此,我们有一个名为Dictionary1.txt的文件,它的旁边紧跟着带有发音的单词。 What I want to do is to get the first word from each line and print them onto another txt file that the program creates from scratch. 我要做的是从每一行中获取第一个单词,并将它们打印到程序从头开始创建的另一个txt文件中。 My code does it but it also prints random Chinese letters in between English words, I don't know why. 我的代码可以做到,但它还会在英文单词之间打印随机的中文字母,我不知道为什么。

Here's what the ouput file looks like: https://imgur.com/a/pZthP 输出文件如下所示: https : //imgur.com/a/pZthP

(Pronounciations are seperated from the actual words in each line with a blankspace in dictionary1.txt) (发音与每行的实际单词分开,在dictionary1.txt中带有空格)

My code: 我的代码:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
char line[100];
int i = 0;
FILE* fp1 = fopen("dictionary1.txt", "r");
FILE* fp2 = fopen("dictionary2.txt", "w");

if (fp1 == NULL || fp2 == NULL){
    printf("ERROR");
    return -1;
}

while (fgets(line, 100, fp1) != NULL){
    while (line[i] != ' '){
        fputc(line[i], fp2);
        i++;
    }
    i=0;
    fputc('\0', fp2);
}

return 0;
}

I tried fputc('\\n', fp2) as well bu t no matter what I couldn't get onto the next line in the file I created from scratch. 我也尝试过fputc('\\ n',fp2),无论我无法从头开始创建文件的下一行如何。 I also can't get rid of all the random Chinese letters. 我也不能摆脱所有随机的中文字母。

EDIT: I figured it out. 编辑:我想通了。 The .txt file I was working on was saved in Unicode formatting, which didn't work well with my program. 我正在处理的.txt文件以Unicode格式保存,但该程序不能很好地与我的程序配合使用。 I turned it into ANSI and now it works like a charm. 我将其转换为ANSI,现在它就像一种魅力。

\\n is not the right line separator on all operating systems and all editors. \\ n在所有操作系统和所有编辑器上都不是正确的行分隔符。

If you are editing your txt files on Notepad, try fputs ("\\r\\n", fp2); 如果要在记事本上编辑txt文件,请尝试使用fputs ("\\r\\n", fp2); , where \\r means carriage return (cursor returns at the first character of the line) and \\n new line. ,其中\\ r表示回车符(光标在该行的第一个字符处返回)和\\ n新行。

Generally speaking, Windows uses '\\r\\n' as line separator, the '\\n' character is displayed as something else than end line, at least in Notepad. 一般来说,Windows使用'\\ r \\ n'作为行分隔符,'\\ n'字符至少在记事本中显示为除结束行以外的其他内容。 Linux and Mac OS use different line separators. Linux和Mac OS使用不同的行分隔符。 You may also want to try fprintf(fp2, "\\n"); 您可能还想尝试fprintf(fp2, "\\n"); Check this out \\n and \\r seem to work everywhere. 核对一下\\ n和\\ r似乎无处不在。 Why is line.separator more portable? 为什么line.separator更可移植?

If you don't mind using C++, you could try to create an output stream os and write os << endl Note that some compilers may automatically convert '\\n' into the corresponding operating system end line character/caracther sequence, whereas some may not. 如果您不介意使用C ++,则可以尝试创建输出流os并编写os << endl请注意,有些编译器可能会自动将'\\ n'转换为相应的操作系统结束符/字符序列,而有些可能不。

Another thing, change the while loop condition into line[i] != ' ' && line[i] != '\\0' and close the file fp2 using fclose . 另一件事,将while循环条件更改为line[i] != ' ' && line[i] != '\\0'然后使用fclose关闭文件fp2

.txt file was saved using Unicode formatting. .txt文件是使用Unicode格式保存的。 I turned it into ANSI and everything was suddenly fixed. 我将其转换为ANSI,所有内容突然修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM