Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
读取c+中的文件时未获得正确的输出+; 我正在帮助一个朋友,一个简单的C++任务,关于读取一个文件并打印它,这里是代码: #include<iostream> #include<cstdlib> #include<fstream> using namespace std; const int P=10; struct persona { string nombre; int puntos; }; typedef persona vec[P]; int main (void) { ifstream f; vec v; int i; f.open("estadisticas2.txt"); if(!f) cout<<"Error abriendo fichero\n"; else { for(i=0;i<P;i++) { getline(f,v[i].nombre); f >> v[i].puntos; f.ignore(); } f.close(); for(i=0;i<P;i++) { cout<<v[i].nombre<<"\n"; cout<<v[i].puntos<<"\n"; } } system("pause"); return 0; }_C++_Iostream_Fstream - Fatal编程技术网

读取c+中的文件时未获得正确的输出+; 我正在帮助一个朋友,一个简单的C++任务,关于读取一个文件并打印它,这里是代码: #include<iostream> #include<cstdlib> #include<fstream> using namespace std; const int P=10; struct persona { string nombre; int puntos; }; typedef persona vec[P]; int main (void) { ifstream f; vec v; int i; f.open("estadisticas2.txt"); if(!f) cout<<"Error abriendo fichero\n"; else { for(i=0;i<P;i++) { getline(f,v[i].nombre); f >> v[i].puntos; f.ignore(); } f.close(); for(i=0;i<P;i++) { cout<<v[i].nombre<<"\n"; cout<<v[i].puntos<<"\n"; } } system("pause"); return 0; }

读取c+中的文件时未获得正确的输出+; 我正在帮助一个朋友,一个简单的C++任务,关于读取一个文件并打印它,这里是代码: #include<iostream> #include<cstdlib> #include<fstream> using namespace std; const int P=10; struct persona { string nombre; int puntos; }; typedef persona vec[P]; int main (void) { ifstream f; vec v; int i; f.open("estadisticas2.txt"); if(!f) cout<<"Error abriendo fichero\n"; else { for(i=0;i<P;i++) { getline(f,v[i].nombre); f >> v[i].puntos; f.ignore(); } f.close(); for(i=0;i<P;i++) { cout<<v[i].nombre<<"\n"; cout<<v[i].puntos<<"\n"; } } system("pause"); return 0; },c++,iostream,fstream,C++,Iostream,Fstream,代替(实际的.txt值): 谢谢你所做的一切 { getline(f, v[i].nombre); string l; getline(f, l); v[i].puntos = stoi(l); } 不知道为什么您的示例不起作用,但我不喜欢将getline与> 另外,您忘记将string包含在您的f.ignore()帐户中,用于丢弃一个字符,您假定该字符是换行符,但在您的输入文件中: unknown 0 pene 20 ojete 40 tulia 240 <=== h

代替(实际的.txt值):

谢谢你所做的一切

{
  getline(f, v[i].nombre);
  string l;
  getline(f, l);
  v[i].puntos = stoi(l);
}
不知道为什么您的示例不起作用,但我不喜欢将getline与
>

另外,您忘记将
string

包含在您的
f.ignore()
帐户中,用于丢弃一个字符,您假定该字符是换行符,但在您的输入文件中:

unknown 
0
pene
20
ojete
40
tulia
240 <=== here
Ano 
2134
lolwut
123
unknown 
0 <=== here
unknown 
0 <=== here
unknown 
0 <=== here
unknown 
0 <=== here
未知
0
佩内
20
奥杰特
40
图利亚

240自从getline刷新新行字符以来,您是否在没有f.ignore()的情况下尝试过它?是的,我尝试过,结果甚至是最糟糕的。如果我错了,请纠正我,但getline返回一个字符串,“puntos”是一个int。@franchojavilo您是否将该行读入一个字符串,然后使用
stoi
解析该字符串以获得它所代表的整数eah,这是一个可行的解决方案,我会尽快尝试,谢谢你的时间,朋友!
{
  getline(f, v[i].nombre);
  string l;
  getline(f, l);
  v[i].puntos = stoi(l);
}
unknown 
0
pene
20
ojete
40
tulia
240 <=== here
Ano 
2134
lolwut
123
unknown 
0 <=== here
unknown 
0 <=== here
unknown 
0 <=== here
unknown 
0 <=== here
f.ignore(std::numeric_limits<std::streamsize>::max(), '\n')