从文件中读取多行字符串并将其存储在C ++中的字符串数组中 [英] Reading multiple lines of strings from a file and storing it in string array in C++

查看:220
本文介绍了从文件中读取多行字符串并将其存储在C ++中的字符串数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为StringList的类,由一个构造函数和一个结构体构成。我正在拍摄的是我的程序能够保持阵列中的字符串,即使它没有运行。我想这样做的方式是让我的构造函数从文件读取字符串并将它们存储到我的字符串数组(str [])中。我的解构器会将我当前的字符串保存到我的文件中。创建内存时,我无法从文件中读取和存储。我希望每个单词都是数组中的一个元素。
例如,在读取的文件中,字符串被存储为:

  HELLO 
MOM
DAD
FOUR
YELLOW

我希望每个字是一个插槽。换一种说法。 str [0] = HELLO,str [1] = MOM,str [2] = DAD等。

这是我的构造函数:

  StringList :: StringList()
{
numberOfStrings = 0;
str = new string [1000000];

ifstream myfile(Read.txt);
if(myfile.is_open())
{
for(int i = 0; i <1000000; i ++)
{
getline(myfile,str [一世]);
numberOfString ++;


}

myfile.close();
}


}

这里的问题是for(int i = 0; i <100000; i ++)line
这样做的作用是继续将每个空格填充到元素中,直到它达到100000.
同样如果我把i <20,它会读取所有内容并添加空格以填充到20.是否有填充txt中实际字符串的数量。 file?

解决方案

NumberOfStrings ++ (即它只会增加一次)。另外请考虑使用 std :: vector 来代替动态数组。



这里有一个使用std :: vector代替数组的代码版本:

  #include< vector> 
#include< fstream>
#include< iostream>
#include< string>
$ b $ class StringList
{
public:
StringList():str(1000000),numberOfStrings(0)
{
std :: ifstream myfile(Read.txt);
if(myfile.is_open())
{
for(int i = 0; i< str.size(); i ++)
{
getline( myfile,str [i]);
numberOfStrings ++;
}

myfile.close();



StringList ::〜StringList()
{
std :: ofstream os(Read.txt);
for(int i = 0; i< numberOfStrings; i ++)
{
os<< str [i]<<的std :: ENDL;
}
}

private:
std :: vector< std :: string>海峡;
int numberOfStrings;
};

正如您所看到的,所做的更改非常少。


I have a class called StringList consisting of a constructor and a de-structor. What I am shooting for is for the ability of my program to retain its strings in the array even after it is not running. The way i want to do this is to have my constructor function read strings from a file and store them into my string array (str[]). My de-structor will save my current strings into my file. I am having trouble reading and storing from the file when memory is created. I want each word to be one element in the array. For example, in the file that is being read from, the strings are stored as such:

HELLO
MOM
DAD
FOUR
YELLOW

I want each word to be a slot. In other words. str[0] = HELLO, str[1]= MOM, str[2]=DAD and such.

Here is my constructor function:

StringList::StringList()
{
    numberOfStrings=0;
    str = new string[1000000];

ifstream myfile ("Read.txt");
 if (myfile.is_open())
    {
        for (int i = 0; i < 1000000; i++)
        {
            getline(myfile,str[i]);
            numberOfString++;


        }

        myfile.close();
    }


}

Problem here is the for (int i=0; i<100000;i++) line What this did is continue to fill each blank space into the element until it reached 100000. Same if i put i<20, it would read all the contents and add blanks to fill to 20. Is there anyway to fill up to the amount of actual strings in the txt. file?

解决方案

NumberOfStrings++ is outside of your for loop when you read (i.e. it only gets incremented once). Also please consider using std::vector<std::string> instead of a dynamic array.

Here's a version of your code using std::vector instead of an array:

#include <vector>
#include <fstream>
#include <iostream>
#include <string>

class StringList
{
public:
    StringList(): str(1000000), numberOfStrings(0)
    {
        std::ifstream myfile ("Read.txt");
        if (myfile.is_open())
        {
            for (int i = 0; i < str.size(); i++)
            {
                getline(myfile, str[i]);
                numberOfStrings++;
            }

            myfile.close();
        }
    }

    StringList::~StringList()
    {
        std::ofstream os("Read.txt");
        for (int i = 0; i <numberOfStrings; i++) 
        {
            os << str[i] << std::endl;
        }
    }

private:
    std::vector<std::string> str;
    int numberOfStrings;
};

As you can see the changes are rather minimal.

这篇关于从文件中读取多行字符串并将其存储在C ++中的字符串数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆