问题描述
这是我写的部分程序,像往常一样,我遇到了问题
中途:
/ * C ++ Primer - 4 / e
*
*练习8.9
*声明:
*写一个函数来打开一个文件进行输入,然后读取它的
coontents到一个字符串向量中,storinig每行作为单独的
元素在向量中。
*
* /
#include< iostream>
#include< fstream>
#include< vector>
#使用指针包含< string>
/ *,因为第一个参数是输入文件流,而第二个参数是
我想采用原始矢量,而不是复制
一。
下一行是错误的来源,第22行* /
void read_file(ifstream * my_file,std :: vector< std :: string> * svec)
{
ifstream infile;
infile.open(" my_file");
/ *流程文件
这里
* /
}
int main()
{
std :: vector< ; std :: stringsvec;
std :: vector< std :: string> * psvec;
std :: cout<< 输入文件的完整路径:; ifstream my_file;
ifstream * p_my_file;
std :: cin> p_my_file;
read_file(p_my_file ,psvec);
返回0;
}
--------输出 - -------------
[arnuld @ Arch64 c ++] $ g ++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
ex_08-09.cpp:22:错误:变量或字段a ?? read_filea ??声明无效
ex_08-09.cpp:22:错误:a ?? ifstreama ??未在此范围内声明
ex_08-09.cpp:22:错误:a ?? my_filea ??未在此范围内声明
ex_08-09.cpp:22:错误:在?? * a ??之前预期的primary-expression令牌
ex_08-09.cpp:22:错误:'svec''未在此范围内声明
ex_08-09.cpp:在函数''int main中()'':
ex_08-09.cpp:39:错误:'ifstream''未在此范围内声明
ex_08-09.cpp:39:错误:预期`;''在?? my_filea之前??
ex_08-09.cpp:40:错误:''p_my_file''未在此范围内声明
ex_08-09.cpp:44:错误:'read_file''未在此范围内声明
[arnuld @ Arch64 c ++] $
打开文件是问题的第一阶段。第二阶段将包括
将每一行作为库字符串复制到向量中。我在第一阶段面临
问题。
-
This is the partial-program i wrote, as usual, i ran into problems
halfway:
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a function to open a file for input and then read its
coontents into a vector of strings, storinig each line as separate
element in vector.
*
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
/* using Pointers because 1st argument is an input file-stream and
for 2nd argument i want to take the original vector, not the copied
one.
next line is the source of error, line #22 */
void read_file( ifstream* my_file, std::vector<std::string>* svec )
{
ifstream infile;
infile.open("my_file");
/* Process File
Here
*/
}
int main()
{
std::vector<std::stringsvec;
std::vector<std::string>* psvec;
std::cout << "Enter the full Path to file: "; ifstream my_file;
ifstream* p_my_file;
std::cin >p_my_file;
read_file( p_my_file, psvec );
return 0;
}
-------- OUTPUT --------------
[arnuld@Arch64 c++]$ g++ -ansi -pedantic -Wall -Wextra ex_08-09.cpp
ex_08-09.cpp:22: error: variable or field a??read_filea?? declared void
ex_08-09.cpp:22: error: a??ifstreama?? was not declared in this scope
ex_08-09.cpp:22: error: a??my_filea?? was not declared in this scope
ex_08-09.cpp:22: error: expected primary-expression before a??*a?? token
ex_08-09.cpp:22: error: ''svec'' was not declared in this scope
ex_08-09.cpp: In function ''int main()'':
ex_08-09.cpp:39: error: ''ifstream'' was not declared in this scope
ex_08-09.cpp:39: error: expected `;'' before a??my_filea??
ex_08-09.cpp:40: error: ''p_my_file'' was not declared in this scope
ex_08-09.cpp:44: error: ''read_file'' was not declared in this scope
[arnuld@Arch64 c++]$
Opening the file is the 1st stage of the problem. 2nd stage will include
copying the each line as a library string into the vector. I am facing
problems at very 1st stage.
--
http://lispmachine.wordpress.com
推荐答案
你的功能有点奇怪。如果第一个arg是输入
文件流,为什么你不使用它?
你创建一个新的ifstream并用文件名调用open?
也许您只想将文件名传递给函数。
或者您希望在此函数后流保持打开状态
结束?
此外,你不需要指向你的向量的指针。可能
最好通过引用传递它。
Your function is a bit strange. If the first arg is an input
file stream, why aren''t you using it?
You create a new ifstream and call open with a file name?
Maybe you just want to pass the file name to the function.
Or do you want the stream to stay open after this function
finishes?
Also, you don''t need a pointer to your vector. Probably
better to pass it by reference.
难道ifstream不能在std命名空间中吗?
Shouldn''t ifstream be in the std namespace?
这篇关于将文本文件读入字符串向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!