beginner question

J

jvax

Hi all,

I hope I'm posting in the right NG...
I have a data text file I want to read from a c++ program.
the data file goes like this:

90 # number of balls
33
42
13
45
23
...
...

I want to store the number in the first line in a variable ommitting
the following comment, then scan the rest and store it in an array.
The array I could get it done with no problem. The first line is a
problem. When I delete the "# number of balls" comment, the following
code works fine, but I need to have the comment after the number.
here's my code so far:

while (!valid) {
cout << "Name of test file: ";
cin >> filename;
#define TESTER_FILE filename
if ((tester = fopen(TESTER_FILE, "r")) == NULL)
cout << "Please enter a valid file." << endl;
else
valid = 1;
}
valid = 0;
fclose(tester);
cout << endl;
int try_with_same_data = 1;
while (try_with_same_data) {
ifstream Test_file (TESTER_FILE, ios::in);
Test_file >> BinSize;
...
...
}

How can I scan the first line, get the number and then go to next
line?

thanks for any help.
 
J

Jim Fischer

jvax said:
Hi all,

I hope I'm posting in the right NG...
I have a data text file I want to read from a c++ program.
the data file goes like this:

90 # number of balls
33
42
13
45
23
..
..

I want to store the number in the first line in a variable ommitting
the following comment,

// Read the first number into the variable 'count'
in >> count;

// Ignore everything up to and including the next newline
// character '\n'. [n.b. In this example, each line of input
// is delimited by a newline character.]
in.ignore(std::numeric_limits<streamsize>::max, '\n');
 
S

Stuart Golodetz

jvax said:
Hi all,

I hope I'm posting in the right NG...
I have a data text file I want to read from a c++ program.
the data file goes like this:

90 # number of balls
33
42
13
45
23
..
..

I want to store the number in the first line in a variable ommitting
the following comment, then scan the rest and store it in an array.
The array I could get it done with no problem. The first line is a
problem. When I delete the "# number of balls" comment, the following
code works fine, but I need to have the comment after the number.
here's my code so far:

while (!valid) {
cout << "Name of test file: ";
cin >> filename;
#define TESTER_FILE filename
if ((tester = fopen(TESTER_FILE, "r")) == NULL)
cout << "Please enter a valid file." << endl;
else
valid = 1;
}
valid = 0;
fclose(tester);
cout << endl;
int try_with_same_data = 1;
while (try_with_same_data) {
ifstream Test_file (TESTER_FILE, ios::in);
Test_file >> BinSize;
...
...
}

How can I scan the first line, get the number and then go to next
line?

thanks for any help.

#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main()
{
std::vector<int> numVec;
std::string sTemp;
std::ifstream fs("Test5.txt"); // put your filename here

std::stringstream ss;
while(std::getline(fs, sTemp))
{
ss << sTemp;
int iTemp;
ss >> iTemp;
numVec.push_back(iTemp);

ss.str("");
ss.clear();
}

// Output numbers in the format "1 2 3 4 ..."
std::copy(numVec.begin(), numVec.end(),
std::eek:stream_iterator<int>(std::cout, " "));

return 0;
}

HTH,

Stuart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

beginner question 6
Syntax error and im a beginner 1
First time question 1
Cannot find my infinite loop 1
Minimum Total Difficulty 0
TF-IDF 1
Rearranging .ply file via C++ String Parsing 0
Identifier not declared? 1

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top