getline和文件 [英] getline and files

查看:85
本文介绍了getline和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很迷茫。我正在读C ++大学课程,首先让我告诉我b $ b状态我不是要求任何人做我的任务,只是

澄清我似乎没有能够领悟到。我有一个

..txt文件,我想读入多维字符串数组。

文件的每一行都需要读入数组。 OK ..声音很简单

足够了,但是我不能让getline(file_name array_name)工作。

所以...我认为它绝对是运算符这是我的错误。

关于我做错了什么建议?


KL

解决方案

KL写道:

关于我做错的任何建议?



请发布你的代码。 />
-

CrayzeeWulf




CrayzeeWulf写道:

KL写道:

关于我做错的任何建议?


请发布你的代码。
-
CrayzeeWulf




是的,这可能会有所帮助!对不起!


#include< iostream>

#include< fstream>

#include< cstdlib>

#include< string>

使用命名空间std;

int main(){

char question [50];

char string [50];

ifstream问题;

questions.open(" qs.txt"); //打开文件以阅读来自的问题

getline(问题,字符串);

while(!questions.eof()){

for(int x = 0; x< 20; x ++){

question [x] = string;

getline(问题,字符串);

}


questions.close();

for(int ind = 0; ind< 20; ind ++){

cout<<问题[ind];

}

}


而且...我仍然无法建立它错误...我一直收到一条

消息:c:\Documents and Settings\Kari-Lyn Bjorn \ My

Documents\UA\2005 Spring\CS114 \Assignments\Project 7 \ Project

7 \\\ 10435569.cpp(33):错误C2784:''std :: basic_istream< _Elem,_Traits>

& std :: getline(std :: basic_istream< _Elem,_Traits>

&,std :: basic_string< _Elem,_Traits,_Alloc>&)'':不能演绎

''std :: basic_istream< _Elem,_Traits>的模板参数&''来自

''std :: ifstream''


这对我来说毫无意义。我留给你的专家指出我的

逻辑等错误(包括错别字,因为我之前已经这样做了

!)


KL


KL写道:

#include< iostream>
#include< fstream>
#include< cstdlib>
#include< string>
使用命名空间std;
int main(){
char question [50] ;
char string [50];
你好KL,


命名一个变量string可能不是一个好主意。这里是因为

C ++ STL包含一个具有相同名称的模板(std :: string)。但是,这个

是一个小问题而不是你的程序有问题。

ifstream问题;
questions.open(" qs.txt"); //打开文件以阅读来自
getline的问题(问题,字符串);
您使用的是getline()这里起作用而不是使用ifstream的相应

方法。函数getline()不会将ifstream

作为其第一个参数。你应该考虑使用类似的东西:


questions.getline(string,50);

while(!questions.eof()){
for(int x = 0; x< 20; x ++){
question [x] = string;

在上面的最后一行,question [x]"有一种char类型而

" string"的类型是char *。您不能指定类型char *的值。到一个

" char"。你想通过上面的循环做什么?


getline(问题,字符串);
}
再一次,在这里使用ifstream :: getline() 。


questions.close();



这是一个坏主意。您正在关闭while()内的ifstream。循环

可能会尝试从中读取更多数据。一旦你完成了while(),你应该关闭ifstream

。循环。

希望有所帮助。

-

CrayzeeWulf


I am so lost. I am in a college course for C++, and first off let me
state I am not asking for anyone to do my assignment, just
clarification on what I seem to not be able to comprehend. I have a
..txt file that I want to read into a multi-dimensional string array.
Each line of the file needs to be read into the array. OK..sounds easy
enough, but I can''t get the getline(file_name array_name) to work.
So...I am thinking it is definitely operator error here on my part.
Any advice on what I am doing wrong?

KL

解决方案

KL wrote:

Any advice on what I am doing wrong?


Please post your code.
--
CrayzeeWulf



CrayzeeWulf wrote:

KL wrote:

Any advice on what I am doing wrong?


Please post your code.
--
CrayzeeWulf



Yeah that probably would be helpful! Sorry!

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
int main(){
char question[50];
char string[50];
ifstream questions;
questions.open("qs.txt");//Opens file to read questions from
getline(questions,string);
while(!questions.eof()){
for(int x=0; x<20; x++){
question[x]=string;
getline(questions,string);
}

questions.close();
for(int ind=0; ind<20; ind++){
cout << question[ind];
}
}

And... I still can''t get it to build without errors...I keep getting a
message of: c:\Documents and Settings\Kari-Lyn Bjorn\My
Documents\UA\2005 Spring\CS114\Assignments\Project 7\Project
7\10435569.cpp(33): error C2784: ''std::basic_istream<_Elem,_Traits>
&std::getline(std::basic_istream<_Elem,_Traits>
&,std::basic_string<_Elem,_Traits,_Alloc> &)'' : could not deduce
template argument for ''std::basic_istream<_Elem,_Traits> &'' from
''std::ifstream''

Which means nothing to me. I leave it to you experts to point out my
errors in logic, etc. (including typos, as I have done that before
too! )

KL


KL wrote:

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
int main(){
char question[50];
char string[50]; Hi KL,

It is probably not a good idea to name a variable "string" here because the
C++ STL contains a template with the same name (std::string). However, this
is a minor point and not a problem with your program.
ifstream questions;
questions.open("qs.txt");//Opens file to read questions from
getline(questions,string); You are using "getline()" function here instead of using the corresponding
method of "ifstream". The function "getline()" does not take an "ifstream"
as its first argument. You should consider using something like:

questions.getline( string, 50 ) ;
while(!questions.eof()){
for(int x=0; x<20; x++){
question[x]=string;
In the last line above, "question[x]" has a type of "char" while the type of
"string" is "char *". You cannot assign a value of type "char *" to a
"char". What are you trying to do through the above loop ?

getline(questions,string);
} Once again, use ifstream::getline() here.


questions.close();


This is a bad idea. You are closing the ifstream inside a "while()" loop
that might attempt to read more data from it. You should close the ifstream
once you are done with the "while()" loop.
Hope that helps.
--
CrayzeeWulf


这篇关于getline和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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