Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c++;文件io? 我在C++中有一个关于文件I/O的编程项目。我已经读过了,但还不太明白。不管怎样,我制作了这个程序,允许用户将一个人的个人资料(姓名、种族等)存储到一个文本文件中,并能够检索它。然而,我在如何工作方面遇到了一些问题_C++ - Fatal编程技术网

如何使用c++;文件io? 我在C++中有一个关于文件I/O的编程项目。我已经读过了,但还不太明白。不管怎样,我制作了这个程序,允许用户将一个人的个人资料(姓名、种族等)存储到一个文本文件中,并能够检索它。然而,我在如何工作方面遇到了一些问题

如何使用c++;文件io? 我在C++中有一个关于文件I/O的编程项目。我已经读过了,但还不太明白。不管怎样,我制作了这个程序,允许用户将一个人的个人资料(姓名、种族等)存储到一个文本文件中,并能够检索它。然而,我在如何工作方面遇到了一些问题,c++,C++,它所做的是在开始时询问用户是否要查看以前创建的文件或创建新文件 如果选择查看以前的文件,则程序将吐出整个文本文件 如果选择创建新文件,程序将询问人数,然后继续询问分配人数的姓名、年龄、性别、物种、种族和交通方式,并将其保存到文本文件中 问题是我无法设置人数,因为程序忽略了该变量(即,我设置了5人,但它忽略了该变量) 程序会跳过输入的名字,忽略它 #include <iostream> #include <fstream> using namespace std; int

它所做的是在开始时询问用户是否要查看以前创建的文件或创建新文件

如果选择查看以前的文件,则程序将吐出整个文本文件

如果选择创建新文件,程序将询问人数,然后继续询问分配人数的姓名、年龄、性别、物种、种族和交通方式,并将其保存到文本文件中

问题是我无法设置人数,因为程序忽略了该变量(即,我设置了5人,但它忽略了该变量) 程序会跳过输入的名字,忽略它

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char names[100];
    char age[100];
    char gender[100];
    char species[100];
    char ethnicity[100];
    char transport[100];
    char decision[0];

    string getcontent;

   cout << "Would You Like to Open The Previous File Or View Your Profiles?" << endl << endl;
   cout << "Enter 1 For Previous File" << endl;
   cout << "Enter Anything Else To View Your Profiles:  " << decision;
   cin.getline(decision,5);
   cout << endl << "==============================================================" << endl;
   if(decision[0] == '1')
   {
       ifstream infile;
       infile.open("File of names.txt");
       while(! infile.eof())//eof stand for end of file
        {
            getline(infile, getcontent); //getline requires a string
            cout << getcontent << endl;
        }
        cout << "==============================================================" << endl;
        infile.close(); //closes the opened file - good practice

   }
   else
   {
       int a;
       cout << "Enter The Amount Of People You Would Like To Store: ";
       cin >> a;
       ofstream namefile;
       namefile.open("File of names.txt");
       cout << "Please Set Your Team Profile." << endl;
       for (int i=0; i<a; i++)
        {
            cout << "==============================================================" << endl;
            cout << "Enter Student " << i+1 << " : ";
            cin.getline(names,100);
            namefile << names << endl;
            cout << "==============================================================" << endl;

            cout << "Enter The Age: ";
            cin.getline(age,100);
            namefile << age << endl;

            cout << "Enter The Gender: ";
            cin.getline(gender,100);
            namefile << gender << endl;

            cout << "Enter The Species: ";
            cin.getline(species,100);
            namefile << species << endl;

            cout << "Enter The Ethnicity: ";
            cin.getline(ethnicity,100);
            namefile << ethnicity << endl;

            cout << "What Is The Mode Of Transport: ";
            cin.getline(transport,100);
            namefile << transport << endl << endl;
        }
namefile.close();


   }

}
这是预期输出:

Would You Like to Open The Previous File Or View Your Profiles?

Enter 1 For Previous File
Enter Anything Else To View Your Profiles: g

==============================================================
Enter The Amount Of People You Would Like To Store: 5
Please Set Your Team Profile.
==============================================================
Enter Student 1: John
==============================================================
Enter The Age:

问题在于,例如,当您输入以下值时:
string-str
cin>>str
如果插入“John”,则不会将“John”分配给str,而是将“John”分配给str(注意\n,这是在键盘上键入enter时创建的)。忽略它的一个可能解决方案是使用
cin.ignore()
然而,你的家庭作业可能是制作一个非常简单的“数据库”,因此你必须以有序的方式记住数据,然后我建议你使用“struct”(对初学者来说很简单,不难)。

相关/重复:
Would You Like to Open The Previous File Or View Your Profiles?

Enter 1 For Previous File
Enter Anything Else To View Your Profiles: g

==============================================================
Enter The Amount Of People You Would Like To Store: 5
Please Set Your Team Profile.
==============================================================
Enter Student 1: John
==============================================================
Enter The Age: