簡體   English   中英

C ++在Linux / Ubuntu中寫入文件錯誤?

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

我最近一直在嘗試學習C ++,但是偶然發現了一些錯誤。 例如,當我嘗試運行此代碼以詢問用戶他們想要輸出到文件的內容時:

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

使用

g++ -o main test.cpp

我收到此錯誤:

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);
                      ^

有人可以幫我嗎? 如果這是可以輕松解決的問題,請原諒我。我對C ++還是陌生的,現在還不太了解。

缺少字符串標題:

#include <string>

如果沒有它,則不會定義sring,並且在任何使用輸出的地方都會出現錯誤

有了include,您將減少很多錯誤。 但是這行還有另一個問題(如πάνταῥεῖ已經指出):

cin.getline(output, 256); 

因為cin.getline()需要一個char*和長度。 如果要使用字符串,則必須在istream上使用不帶大小(限制為最大字符串大小)的getline()函數:

 getline(cin, output); 

最后一點: 您當然可以隨意混合使用c風格的io和流。 但是,您可以習慣於為所有文件io進行流傳輸,從而贏得勝利。

錯誤發生在該行

cin.getline(output, 256);

根據std :: istream :: getline的文檔, cin.getline()的第一個參數應該是char *而不是您聲明的std::string

嘗試像這樣將輸出聲明更改為char *

char[256] output;

編輯:使用std::getline就像其他人所說的那樣是一個更好的主意。

暫無
暫無

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

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