c++ - 在其他函数中获取变量后创建数组

标签 c++ arrays file memory-management dynamic-memory-allocation

文本文件如下所示:

3
String
String2
String3

我必须创建一个函数来从文本文件中读取所有字符串,将它们保存到数组中,然后在主函数中显示它。例如

void reading(int & count, string strings[]) {
ifstream fd "text.txt";
fd >> count;
for (int i=0; i<count; i++)
fd >> strings[i];
fd.close();

主要功能:

int main() {
int count=0;
string strings[100];
reading(count, strings);
for (int i=0; i<count; i++) 
cout << strings[i] << endl;

字符串的数量写在文本文件的第一行。如何创建一个恰好包含该数字的数组?我需要它能够在主要/其他功能中访问它。 (例如写入另一个文本文件的函数)。

最佳答案

如果有一个非常重要的原因要用数组来做,那么使用 std::new ,像这样:

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

int reading(string** str_array, int& size,  string filename) {
    ifstream infile(filename.c_str());
    if(!infile) {
        cerr << "Unable to access file!\n";
        return -1;
    }
    int n = 0;
    infile >> size;
    try{
        *str_array = new string[size];
        string str;
        for (; n < size && infile; ++n) {
            infile >> str;
            (*str_array)[n] = str;
        }

        if (n != size)
            cerr << "ERROR, read less than " << size << " strings!!\n\n";
    } catch(bad_alloc& exc) {
        return -2;
    }
    return 0;
}


int main() {
    string* str_array = NULL;
    int size;
    if(reading(&str_array, size, "test.txt")) {
        cerr << "Din't read file, exiting...\n";
        return -1;
    }
    for(int i = 0; i < size; ++i)
        cout << str_array[i] << endl;

    delete [] str_array; // DO NOT FORGET TO FREE YOUR MEMORY
    str_array = NULL;

    return 0;
}

输出:

C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
String
String2
String3

然而,你在而且您没有使用 std::vector为此?

看看这有多简单:

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

int reading(vector<string>& v, string filename) {
    ifstream infile(filename.c_str());
    if(!infile) {
        cerr << "Unable to access file!\n";
        return -1;
    }
    int N = -1, n = 0;
    infile >> N;
    string str;
    for (; n < N && infile; ++n) {
        infile >> str;
        v.push_back(str);
    }

    if (n != N)
        cerr << "ERROR, read less than " << N << " strings!!\n\n";
    return 0;
}

int main() {
    vector<string> v;
    if(reading(v, "test.txt")) {
        cerr << "Din't read file, exiting...\n";
        return -1;
    }
    for(size_t i = 0; i < v.size(); ++i)
        cout << v[i] << "\n";
    return 0;
}

输出:

C02QT2UBFVH6-lm:~ gsamaras$ ./a.out
String
String2
String3

编辑:

我们必须传递一个指向我们要修改的内容的指针(即string*),否则我们应用的更改将不会发生。自己测试一下,传递一个 string* 作为参数而不是 string**,修改函数体,然后她会发生什么。

为了理解这个想法,假设我们想要写入指针,新地址,new 给了我们并保存请求的内存。我们确实在函数内部写了那个地址,但是当函数终止时,我们希望这些变化是持久的。看我的Functions in C举个例子。

关于c++ - 在其他函数中获取变量后创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38667593/

相关文章:

java - 多态性和instanceof

javascript - 按属性在 JSON 中搜索的最简单方法是什么?

vb.net - 对齐输出到文件的文本?列

c++ - C/C++ 中的跨平台自定义文件头

c++ - 通过引用同一 vector 的元素插入 vector

c++ - 显式从头文件调用变量

c++ - 在 glPushMatrix 内部和外部实现 glulookat?

javascript - 数组内具有特定属性的 polymer 计数元素

file - 非 ANSI 文件的 TStringList 行为

c++ - 为什么 std::forward 将左值和右值转换为右值引用?