0

だから、私は自分のプログラムのテキストを表示するのを妨げているプログラムのこのバグを理解しようとして本当に行き詰まっています..

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;

int main ()
{
ifstream infile;
ofstream offile;

char text[1024];
cout <<"Please enter the name of the file: \n";
cin >> text;

infile.open(text);

string scores; // this lines...

getline(infile, scores, '\0'); // is what I'm using...

cout << scores << endl; // to display the file...

string name1;
int name2;
string name3;
int name4;
infile >> name1;
infile >> name2;
infile >> name3;
infile >> name4;

cout << "these two individual with their age add are" << name2 + name4 <<endl;

// 23 + 27

//the result I get is a bunch of numbers...

return 0;

}

ファイルを表示するために使用できる、よりクリーンで簡単な方法はありますか?

ファイルがループで開かれているため、インターネットのすべての方法を理解したり追跡したりするのは困難です..

ファイルの名前を入力してファイルを表示するプログラムが必要です。ファイルには次のものが含まれます...

jack 23 
smith 27

また、ファイルからデータを取得する必要があります。上記のコードを使用して、ファイルからその情報を取得しています...

4

2 に答える 2

0

私は個人的に stringstreams を使用して、一度に 1 行ずつ読み取り、解析します。

例えば:

#include <fstream>
#include <stringstream>
#include <string>

std::string filename;

// Get name of your file
std::cout << "Enter the name of your file ";
std::cin >> filename;

// Open it
std::ifstream infs( filename );
std::string line;

getline( infs, line );

while( infs.good() ) {
    std::istringstream lineStream( line );
    std::string name;
    int age;
    lineStream >> name >> age;
    std::cout << "Name = " << name << " age = " << age << std::endl;

    getline( infs, line );
}
于 2013-07-19T16:20:58.397 に答える