By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

beginner question

P: n/a
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.

--
jvax
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
jvax wrote:
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');

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #2

P: n/a
Call that beginner? If thats beginner......I'm a complete virgin.

"jvax" <jv**@hotmail.com> wrote in message
news:b7**************************@posting.google.c om...
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.

--
jvax

Jul 19 '05 #3

P: n/a
"jvax" <jv**@hotmail.com> wrote in message
news:b7**************************@posting.google.c om...
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.

--
jvax


#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::ostream_iterator<int>(std::cout, " "));

return 0;
}

HTH,

Stuart.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.