Io 如何使用C+在文本文件中插入新行+; 我必须编写一个C++程序,要求用户输入行并存储在文本文件中。 void create_file(char name[80]) { char line[80],op; ofstream fout(name

Io 如何使用C+在文本文件中插入新行+; 我必须编写一个C++程序,要求用户输入行并存储在文本文件中。 void create_file(char name[80]) { char line[80],op; ofstream fout(name,io,turbo-c++,Io,Turbo C++,如何使用C+在文本文件中插入新行+; 我必须编写一个C++程序,要求用户输入行并存储在文本文件中。 void create_file(char name[80]) { char line[80],op; ofstream fout(name); do { cout << "Enter the line you want to enter in the file:" << endl << endl; gets(line);

如何使用C+在文本文件中插入新行+; 我必须编写一个C++程序,要求用户输入行并存储在文本文件中。
void create_file(char name[80])
{
char line[80],op;
ofstream fout(name);
do
{
    cout << "Enter the line you want to enter in the file:" << endl << endl;
    gets(line);
    fout << line << endl;
    cout << "\nDo you want to enter another line?" << endl;
    cin >> op;
}
while(tolower(op) == 'y');
cout << "File created successfully!" << endl;
fout.close();
}
void创建_文件(字符名[80])
{
字符行[80],op;
流式流量计(名称);
做
{

可能不相关,但使用
std::string行;
代替
char行[80]
std::getline(std::cin,line);
代替
get(line);
。如果打开输出文件并以十六进制格式查看它的行之间有什么分隔符:10或13或13,10或10,13?通常是
0x13,0x10
0D 0A
以十六进制格式保存,因此您要么不按应保存行分隔符,要么您的查看器无法正确识别它…@TEDLYNMO IIRC旧turbo中没有std…std是只在Turbo C++之后创建made@Spektre这可能是正确的,但我想我已经看到了Turbo C++的其他用户使用代码> STD< /Cord>,所以他们希望使用一个更新的版本。在读取文件时不要使用<代码>((.FIN.EFF))< /Cord>。
然后您也可以删除循环内的
eof
测试。
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
void show(char name[80])
{
    char line[800];
    cout << "Contents of the file:" << endl << endl;
    ifstream fin (name);
    while(!fin.eof())
    {
        fin.getline(line,80);
        if(fin.eof())
            break;
        cout << line;
    }
}
void create_file(char name[80])
{
    char line[80],op;
    ofstream fout(name);
    do
    {
        cout << "Enter the line you want to enter in the file:" << endl << endl;
        fflush(stdin);
        gets(line);
        fout << line << endl;
        cout << "\nDo you want to enter another line?" << endl;
        fflush(stdin);
        cin >> op;
    }
    while(tolower(op) == 'y');
    cout << "File created successfully!" << endl;
    fout.close();
    show(name);
}
int main()
{
    char name1[80];
    cout <<"Enter the name of the text file:" << endl;
    gets(name1);
    create_file(name1);
    return 0;
}