c++ - 从 'int' 到 'char *' 的无效转换

标签 c++ parameters multidimensional-array fstream getline

我想编写一个程序,该程序将从文本文件中读取并使用结构存储文本文件中的内容,然后重新组合并打印出文本文件中的信息。但是我遇到了 getline 的问题.我试着写 getline像这样

getline(infile, info.name)

但它不起作用。我还包括 <string><cstring>但我仍然遇到像

这样的错误

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'int' to 'char *'

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'char [10][80]' to 'char *'

我想打印出来的文本文件是下面的文本

Isabella Chan
Republic of Singapore
Libra 23 - 10 - 1993
7
I wish to be good in c++
I wish that christina grimmie will win the voice
I wish that ......

对于菜鸟问题​​深表歉意,在此先感谢您!

   #include <iomanip>
   #include <iostream>
   #include <cstdlib>
   #include <ctime>
   #include <cctype>
   #include <fstream>
   #include <string>

   using namespace std;

   const int MAX = 80;
   const int MAXNO = 10;
   enum Zodiac 
   {
            Aquarius, Pisces, Aries, Taurus,
            Gemini, Cancer, Leo, Virgo,
            Libra, Scorpio, Sagittarius, Capricorn
   };
   struct Date
   {
       Zodiac sign;
       int day;
       int month;
       int year;
   };
   struct Student
   {
           char name [MAX];
       char nationality [MAX];
       Date birthDate;
       int no; // no of other messages
       char wishMessage [MAXNO][MAX];
       // Feel free to add in more features
   };

   void myInfo (fstream&, char [], Student&);
   // The above function reads information from the text file
   // and store the information in a structure reference parameter

   void printOut(Student);

   int main()
   {
       fstream infile;
       char fName[MAX];
       Student info;
       cout << "Enter your info file name: "
       cin  >> fName; 
       cout << endl;

       myInfo(infile, fName, info);
       printOut(info);

   }

   void myInfo (fstream& infile, char fName[], Student& info)
   {
          infile.open(fName, ios::in);

      if(!infile)
      {
           cout << "Error file not found!" << endl;
           exit(0);
      }
       infile.getline(info.name, MAX);
       infile.getline(info.nationality,MAX);
       infile  << info.birthDate.sign
           << info.birthDate.day
           << info.birthDate.month
           << info.birthDate.year;
       infile.getline(info.no, MAX);
       infile.getline(info.wishMessage, MAXNO, MAX);

       infile.close();
       cout << "Successfully readed!" << endl;

   }

   void printOut(Student info)
   {
       cout << "My name is " << info.name << endl;
       cout << "My nationality is " << info.nationality << endl;
       cout << "My date of birth is " << info.birthDate.day 
            << " " << info.birthDate.month << " " 
        << info.birthDate.year << endl;
       cout << "I am " << info.birthDate.sign << endl;
       cout << "\n I have " << info.no << " wishes:" << endl;

   }

最佳答案

您似乎正在尝试使用 getline 读入非字符串,而它正在按照 documentation 读入字符串.

Extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character).

这里有两行有问题的行:

infile.getline(info.no, MAX);

infile.getline(info.wishMessage, MAXNO, MAX);

前者读入int,后者读入string数组。

需要先将字符串读入,然后根据需要进行相应的转换操作。

关于c++ - 从 'int' 到 'char *' 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890163/

相关文章:

c++ - PNG Gamma 校正

c++ - 在 linux/sys/class/gpio 中写入文件的错误

将代码从 'hard-coding' 转换为 'soft-coding'

c++ - 如何将 Lambda 函数作为参数传递

javascript - 如何合并多维数组?

c++ - 使用 BOOST ASIO 缓冲器

c++ - readlink 将 errno 设置为 ENOENT

objective-c - 直接通过名称绑定(bind) SQLite 参数

python - 如何获取二维列表中的每个第一个元素

php - 对数据行进行分组,在组内维护一个id的子数组,只呈现每组中最低的id作为一级key