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

massacre123

文本文件如下所示:

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*而不是将a作为参数传递string**,修改函数的主体,然后会发生什么。

要想出这个主意,想象一下我们想向指针写入新地址,该地址new给了我们并保存了所请求的内存。我们确实在该函数中写入了该地址,但是当函数终止时,我们希望更改是持久的。以我的C语言函数为例。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使变量在其他函数中可用

在其他函数中访问变量

[Python]在其他函数中调用的数组函数

使用在其他类扩展中创建的变量

Java Set和Get变量在其他函数中

jQuery - 从其他函数中获取变量

如何在其他函数中获取deviceValue?

如果未在函数范围内声明,PHP 不会在其他函数中获取数组值

将值写入其他变量后,函数中的 C 自由 int 数组

在其他函数中定义函数

在其他变量的函数中访问数据的变量 - 未定义

在不使用全局函数的情况下在其他函数中调用变量

Python - 在其他函数中使用一个函数中的一个变量

Python 多处理:“self.”变量未在其他函数中/由其他函数更新?

在其他函数完成后运行函数

C-使用fwrite在其他结构数组中创建结构数组的bin文件

从函数传递数组并在其他函数中全局使用它?

从其他函数中获取变量值 Python

在其他方法中的方法中创建的访问变量

在其他控制器中更改后变量未更新

在其他类中操作后返回一个变量

在其他函数中声明的变量在主python 3中不起作用?

在构造函数中设置的变量在其他方法中未定义

Python:在模块函数中创建的类实例在其他模块中不可见

如何创建可以在其他模块中编辑而又不全局的变量?

如何在其他函数中使用函数中的变量而不将变量放入类的 __init__ 方法中?

在Javascript中,如何从该函数中调用但在其他位置定义的函数中引用函数范围的变量?

在其他组件中更改输入变量时执行函数

在其他函数中传递局部变量的值时出错