简体   繁体   中英

Writing lines to .txt file c++

For all you programmers out there! I'm trying to figure out why my program won't work. I'm stumped! I'm trying to write a program to open a text file named "CSC2134.TXT" for output, then accept lines of text from the console and write the lines of text to the file and use any empty string to end the program. Here's what I have:

#include <iostream> 
#include <fstream> 
#include <cstring> 
using namespace std; 

int main() 
{ 
 char str[80]; 

 ofstream file1; 
 file1.open("CSC2134.txt"); 

 if (file1 == 0) 
  { 
    cout << "error opening CSC2134.txt" << endl; 
    return 1; 
  } 
 else 
  { 
   file1 << "Enter some text:\n"; 
   while(strlen(str) != '\n') 
    { 
     file1 << cin.getline(str,80); 

     if(strlen(str) == 0) 
     break; 
    } 
   file1.close(); 
  } 

  return 0; 
} 

I'm trying to figure out why I'm getting the error message.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main (){
    ofstream myfile("CSC2134.txt");

    if(myfile.is_open())
    {
        string str;
        do{
            getline(cin, str);
            myfile<<str<< endl;
        }while(str!="");
        myfile.close();
    }
    else cerr<<"Unable to open file";

    return 0;
}

You have a few mistakes:

You are outputting the "enter some text" to the file instead of to cout.

You are not looping in the correct manner, so as to only exit the app if a user's input is empty string.

Here's a corrected version:

#include <iostream> 
#include <fstream> 
#include <cstring> 
using namespace std; 

int main() 
{ 
 char str[80]; 

 fstream file1; 
 file1.open("CSC2134.txt"); 

 if (!file1.is_open()) 
  { 
    cout << "error opening CSC2134.txt" << endl; 
    return 1; 
  } 
 else 
  { 
   std::cout<< "Enter some text:\n"; 

   cin.getline(str,80);
   while((strlen(str) != 0) ) 
    { 

     file1 << str;
     cin.getline(str,80);

    } 
   file1.close(); 
  } 

  return 0; 
} 

Update:

Run this instead, and tell me what the output is when you run your program:

#include <iostream> 
#include <fstream> 
#include <cstring> 
using namespace std; 

int main() 
{ 
 char str[80]; 

 ofstream file1; 

 file1.exceptions ( ifstream::failbit | ifstream::badbit );
 try {
  file1.open("CSC2134.txt", fstream::in | fstream::out | fstream::binary);
 }
 catch (ifstream::failure e) {
    cout << "Exception opening/reading file"<< e.what();
  }

 if (!file1.is_open()) 
  { 
    cout << "error opening CSC2134.txt" << endl; 
    return 1; 
  } 
 else 
  { 
   std::cout<< "Enter some text:\n"; 

   cin.getline(str,80);
   while((strlen(str) != 0) ) 
    { 

     file1 << str;
     cin.getline(str,80);

    } 
   file1.close(); 
  } 

  return 0; 
} 

Here's a version with mistakes and ungood practices corrected:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>     // EXIT_FAILURE
using namespace std;

int main()
{
    auto const filename = "CSC2134.txt";
    ofstream file1( filename );
    if( file1.fail() )
    {
        cerr << "!Error opening " << filename << endl;
        return EXIT_FAILURE;
    }

    string str;
    cout << "Enter some text, with a blank last line:\n";
    while( getline( cin, str ) && str != "" )
    {
        file1 << str << endl;
    }
}

Personally I would write and instead of && , but a beginner can't be expected to configure compiler correctly to accept that. The problem is mainly with Visual C++. One can use a forced include of <iso646.h> to make it accept standard and , or and not .

Tip: I used the free AStyle program to fix the indentation to something more clear to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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