简体   繁体   English

如何读取文件行并存储它们? C ++

[英]How to read file lines and store them? C++

So far I am able to open a txt file and store them as variables. 到目前为止,我已经能够打开一个txt文件并将其存储为变量。

My txt file looks like this 我的txt文件看起来像这样

Tim, 24, Male

I have been able to store them as variables such as name , age , gender 我已经能够将它们存储为变量,例如nameagegender

This is my code 这是我的代码

ifstream inputfiles ("test.txt");
    if(!inputfiles.is_open())
    {

    }
    else
    {
        while(inputfiles >> name >> age >> gender)
        {
            cout << name << "\n";
            cout << age << "\n";
            cout << gender << "\n";
        }

However, my code doesn't store the values as variables when my txt file looks like this... 但是,当我的txt文件看起来像这样时,我的代码不会将值存储为变量...

Tim
24
Male

How do I modify my code such that it can read my file line by line and store it in its variables? 如何修改代码,使其可以逐行读取文件并将其存储在变量中?

If I am right, You want to store it like: 如果我是对的,您想像这样存储它:

  1. Tim 24 Male 蒂姆24男
  2. John 25 Male 约翰25男

Use a class 使用课程

    class data
    {
        public:
        char name[10],gen;
        int age;
        void getdata()
        {
            cout<<"Enter Name";
            gets(name);
            cout<<Enter age";
            cin>>age;
            cout<<"enter gender";
            cin>>gen;
        }
        void putdata()//use cout statements here
        {
        //put cout statements
        }
    };

Now in your main function,use write functon 现在在您的主要功能中,使用write functon

fstream f;
f.open("YourFile.txt",ios::in|ios::out);    
data r;    
for(i=0;i<10;i++)
{
    r.getdata();
    f.write((char*)&r,sizeof(r));
}

Remember,always use read function to print the database values if you used write. 请记住,如果使用写操作,则始终使用读取功能来打印数据库值。

f.seekg(0,ios::beg);
while(!f.eof())
{
   f.read((char*)&r,sizeof(r));
}

If you want to store the text as it is without conversion...use put function. 如果您想直接存储文本而不进行转换...请使用put函数。

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

相关问题 C++:如何从文件中读取多行直到某个字符,将它们存储在数组中并转到文件的下一部分 - C++: How to read multiple lines from file until a certain character, store them in array and move on to the next part of the file 从文本文件中读取行数并将其存储为数组大小C ++的常量int - Read amount of lines from a text file and store them as a constant int for array size c++ 如何从.csv文件中读取值并将其存储在向量中? - c++ How can I read values from a .csv file and store them in a vector? 从文件中读取行并将它们分成两个数字并执行计算并将它们写入文件 c++ - read lines from file and split them into two numbers and perform calculations and write them into file c++ 如何在 c++ 中的文件中写入多行并读取这些行? - How to write multiple lines and read these lines from file in c++? 如何遍历 txt 文件并将行存储在变量 c++ 中 - how to iterate over txt file and store lines in variables c++ 如何读取txt文件C ++并将其拆分为列 - How to read txt file C++ and split them into columns C++如何从文件中只读取几行 - C++ how to read only few lines from a file C ++-如何从具有必须忽略的行的文件中读取 - C++ - How to read from a file that has lines that have to be ignored C ++如何读取文件并拆分行 - c++ how to read a file and split it's lines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM