简体   繁体   English

C ++在Linux / Ubuntu中写入文件错误?

[英]C++ writing to file error in Linux/Ubuntu?

I have been trying to learn C++ recently, but I have stumbled across some errors. 我最近一直在尝试学习C ++,但是偶然发现了一些错误。 For example, when I try to run this code to ask the user what they want outputted to a file: 例如,当我尝试运行此代码以询问用户他们想要输出到文件的内容时:

#include <iostream>
#include <cstdio>

using namespace std;

    main() {
        string output; //Declare variables before starting
        FILE * file = fopen("newfile.txt","w"); //creates file
        cout << "Entire something that you want to be written to the file: " << endl;
        cin.getline(output, 256);  //Asks what you want to put into file
        fprintf(file, output); //Puts output into file
        fclose(file);   //closes file
        return 0;
    }

using 使用

g++ -o main test.cpp

I get this error: 我收到此错误:

test.cpp: In function ‘int main()’:
test.cpp:10:25: error: no matching function for call to ‘std::basic_istream<char>::getline(std::string&, int)’
  cin.getline(output, 256);
                         ^
test.cpp:10:25: note: candidates are:
In file included from /usr/include/c++/4.8/iostream:40:0,
                 from test.cpp:1:
/usr/include/c++/4.8/istream:618:5: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]
     basic_istream<char>::
     ^
/usr/include/c++/4.8/istream:618:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/4.8/istream:427:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]
       getline(char_type* __s, streamsize __n)
       ^
/usr/include/c++/4.8/istream:427:7: note:   no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘std::basic_istream<char>::char_type* {aka char*}’
test.cpp:11:22: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘2’ to ‘int fprintf(FILE*, const char*, ...)’
  fprintf(file, output);
                      ^

Could someone please help me? 有人可以帮我吗? And please forgive me if this is something that can be easily solved, I am fairly new to C++ and do not quite understand it yet. 如果这是可以轻松解决的问题,请原谅我。我对C ++还是陌生的,现在还不太了解。

The header for string is missing: 缺少字符串标题:

#include <string>

Without it, sring isn't defined, and everywhere you use output, you'll have errors 如果没有它,则不会定义sring,并且在任何使用输出的地方都会出现错误

With the include you'll have a lot less errors. 有了include,您将减少很多错误。 But this line has another issue (as πάντα ῥεῖ already pointed out): 但是这行还有另一个问题(如πάνταῥεῖ已经指出):

cin.getline(output, 256); 

because cin.getline() expects a char* and the length. 因为cin.getline()需要一个char*和长度。 If you want to use a string, you have to use the function getline() , without size (limited to strings maximume size) and on an istream: 如果要使用字符串,则必须在istream上使用不带大小(限制为最大字符串大小)的getline()函数:

 getline(cin, output); 

Last remark: you are of course free to mix c-style io and streams. 最后一点: 您当然可以随意混合使用c风格的io和流。 But you could win from getting used to streams for all your file io . 但是,您可以习惯于为所有文件io进行流传输,从而赢得胜利。

The error occurs at the line 错误发生在该行

cin.getline(output, 256);

According to the documentation for std::istream::getline , the first argument for cin.getline() should be a char * and not a std::string as you have declared it. 根据std :: istream :: getline的文档, cin.getline()的第一个参数应该是char *而不是您声明的std::string

Try changing the declaration of output to a char * like so 尝试像这样将输出声明更改为char *

char[256] output;

Edit: Using std::getline as the others have said would be a better idea though. 编辑:使用std::getline就像其他人所说的那样是一个更好的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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