如何将.txt文件作为不同 cpp 中函数的参数传递
How to pass a .txt file as an argument for a function in a different cpp
int main()
{
fstream file;
file.open("new_file.txt" , ios::app);
if (!file.is_open()){
cout << "File does not exist yet !n";
return 1;
}
string input;
cout << "Add new line or edit? Write NEW or EDIT";
cin >> input;
if (input == "NEW")
{
add_new_info();
}
//.....
}
在另一个 cpp 中,我有:
int add_new_info()
{
string aux;
int count;
cout << "Add line ID n";
cin >> aux;
file << aux << "; ";
//...
}
所以基本上我想在main
中打开txt
文件,然后将其传递给add_new_info()
。如何将txt file
作为参数传递给另一个.cpp
中的函数?
您可以只传递对打开文件的引用
int add_new_info(fstream& file)
{
// add info to the file
}
并在main
的电话
add_new_info(file);
就像 zett42 在评论中提到的那样,如果该函数不使用特定于文件流的任何内容,则使用参数 std::ostream&
也将允许将该函数用于其他类型的流,例如add_new_info(cout);
在控制台上显示信息。
相关文章:
- 内置函数可查看CPP中的成员变量
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 我需要知道编译器如何在cpp中使用析构函数
- 使用cpp中的结构和函数的多个学生条目
- 从 c 或 cpp 系列子函数返回到主函数
- 如何使用connect将qml按钮与同一类的cpp函数连接起来
- 在模板化的 Rcpp 函数中调用另一个 cpp 函数
- 共享对象中的符号名称与文件中.cpp函数不同
- 从 python ctypes 调用 CPP 函数
- CPP 函数无法获得正确的输出
- 如何从C文件中调用Cpp函数
- cpp函数来检索各种数据类型的值
- 从 Python 调用 cpp 函数时出现分段错误
- RcppEigen - 从内联到包中的.cpp函数和"Map"
- 如何在将函数作为参数传递给文件中.cpp函数模板时声明
- 正则表达式:获取 CPP 函数 (c#) 的单独参数和参数类型
- "not in this scope" cpp 函数调用中的错误
- 是否有任何cpp函数或对象(不包括从c继承的)不是线程安全的,即使每个线程对自己的数据进行操作
- 将cpp函数转换为在r中使用
- 如何操作cpp函数