将行写入.txt文件c++

3

针对所有的程序员们!我正在尝试找出为什么我的程序无法正常工作。我被难住了!我试图编写一个程序,以输出的方式打开名为“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; 
} 

我正在尝试弄清楚为什么会收到错误信息。


1
什么错误信息? - Cheers and hth. - Alf
你使用的是哪个开发工具?你是否通过该工具运行程序?通常情况下,程序运行的位置并不是你期望的或者你放置文件的位置。例如,Eclipse会在根项目文件夹中查找没有完全限定路径的文件,而不是在可执行文件所在的同一文件夹中查找。 - user4581301
检查程序所在目录的权限。从该目录发出“ls -lad .”命令。如果您没有该目录的写入权限,则程序无法打开文件。您需要为自己授予该目录的写入权限。 - wizurd
我使用CodeBlocks,但由于我有Norton(哈哈经常认为我的程序是“高风险病毒”),因此我使用cpp.sh进行编译(如果Norton不在Codeblocks上阻止它)。 - 3pic1namie6
谢谢。我可以在另一个程序中使用它吗?对不起,我不是故意刁难。这是我第一次接触大多数这些术语。 - 3pic1namie6
显示剩余5条评论
3个回答

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

Windows 7 Home Premium,但我已升级到Windows 10 Home。感谢您的所有帮助。 - 3pic1namie6
我找不到CSC2134.txt文件。 - 3pic1namie6
成功了!!!诺顿干扰了执行过程。非常感谢您的时间和耐心!!! - 3pic1namie6
你好!一开始很难,所以现在我们很高兴。 :) - Dániel Sándor
真的是这样。非常非常感谢!是的,我是!现在我可以睡觉了。 - 3pic1namie6
显示剩余6条评论

2

您有一些错误:

您将“输入一些文本”输出到文件而不是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; 
} 

建议使用 while ( cin.getline(str,80) && strlen(str) != 0 )。否则,while 循环可能会陷入无限循环。 - R Sahu
由于某些原因,我仍然收到错误消息 :'( - 3pic1namie6
1
@3pic1namie6:这可能是因为您没有在可执行文件所在的目录上拥有写入权限。 - wizurd
好的。我找到了目录。谢谢。接下来呢? - 3pic1namie6
非常感谢!你的程序成功了!由于诺顿的原因,它之前无法正确执行。再次感谢你的耐心:D - 3pic1namie6
显示剩余4条评论

2

以下是已更正错误和不良做法的版本:

#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>来使其接受标准的andornot

提示:我使用了免费的AStyle程序将缩进修正为更清晰的格式。


天啊,你想让你的代码易读吗?但工作安全呢?通过晦涩难懂来保证安全啊,先生!通过晦涩难懂来保证安全! - user4581301
我的教练从未向我介绍过cerr或std.lib,所以就像你说的那样,我是个新手。一个彻底的新手!谢谢。 - 3pic1namie6

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接