簡體   English   中英

將行寫入.txt文件C ++

[英]Writing lines to .txt file c++

對於所有在那里的程序員! 我試圖弄清楚為什么我的程序無法正常工作。 我很沮喪! 我試圖編寫一個程序來打開一個名為“ CSC2134.TXT”的文本文件以進行輸出,然后從控制台接受文本行並將文本行寫入該文件,並使用任何空字符串結束該程序。 這是我所擁有的:

#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; 
} 

我試圖弄清楚為什么我收到錯誤消息。

#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;
}

您有一些錯誤:

您正在將“輸入一些文本”輸出到文件,而不是cout。

您的循環方式不正確,因此只有在用戶輸入的字符串為空時才退出應用程序。

這是一個更正的版本:

#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; 
} 

更新:

改為運行此命令,並告訴我運行程序時的輸出是什么:

#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; 
} 

這是一個已糾正錯誤和不良做法的版本:

#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;
    }
}

我個人會寫and而不是&& ,但是不能期望初學者正確配置編譯器來接受它。 問題主要在於Visual C ++。 可以使用<iso646.h>的強制包含來使其接受標准andor not接受。

提示:我使用免費的AStyle程序將縮進修復為我更清楚的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM