我要逐行读取一个txt文件,每行存储在一个不同的变量中:下面是我要读取的txt文件:
java javascript
python
c c++
我想把那个txt文件的内容作为一行写入另一个txt文件:
java javascript python c c++
这是我实现的代码:
#include <stdio.h>
int main(int argc, char *argv[])
{
// read any text file from currect directory
char const *const fileName = "cppbuzz1.txt";
char ch;
FILE *file = fopen(fileName, "r");
FILE *fout;
if (!file)
{
printf("n Unable to open : %s ", fileName);
return -1;
}
fout = fopen("output.txt", "w");
ch = fgetc(file);
while (ch != EOF)
{
fputc(ch, fout);
ch = fgetc(file);
}
fclose(file);
return 0;
}
我无法得到所需的输出文件。我错过了什么?
过滤器换行
#include <stdio.h>
int main(int argc, char *argv[])
{
// read any text file from currect directory
char const *const fileName = "cppbuzz1.txt";
char ch;
FILE *file = fopen(fileName, "r");
FILE *fout;
if (!file)
{
printf("n Unable to open : %s ", fileName);
return -1;
}
fout = fopen("output.txt", "w");
ch = fgetc(file);
while (ch != EOF)
{
//change line break to space
if (ch == 'n')
{
fputc(' ', fout);
}
else
{
fputc(ch, fout);
}
ch = fgetc(file);
}
fclose(file);
fclose(fout); //close output file
return 0;
}