473,549 Members | 2,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

beginner question

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_FI LE, "r")) == NULL)
cout << "Please enter a valid file." << endl;
else
valid = 1;
}
valid = 0;
fclose(tester);
cout << endl;
int try_with_same_d ata = 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
3 2968
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>::ma x, '\n');

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link58 09{at}now.here. com
Jul 19 '05 #2
Call that beginner? If thats beginner......I 'm a complete virgin.

"jvax" <jv**@hotmail.c om> wrote in message
news:b7******** *************** ***@posting.goo gle.com...
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_FI LE, "r")) == NULL)
cout << "Please enter a valid file." << endl;
else
valid = 1;
}
valid = 0;
fclose(tester);
cout << endl;
int try_with_same_d ata = 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
"jvax" <jv**@hotmail.c om> wrote in message
news:b7******** *************** ***@posting.goo gle.com...
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_FI LE, "r")) == NULL)
cout << "Please enter a valid file." << endl;
else
valid = 1;
}
valid = 0;
fclose(tester);
cout << endl;
int try_with_same_d ata = 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::stringstre am ss;
while(std::getl ine(fs, sTemp))
{
ss << sTemp;
int iTemp;
ss >> iTemp;
numVec.push_bac k(iTemp);

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

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

return 0;
}

HTH,

Stuart.
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2541
by: Svens | last post by:
Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if there was a similar funtion in python. For example: (log65536)/(log4)= 8 I've searched around a bit and haven't been able to find anything.
1
2600
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am experimenting a bit with what I need to do this and have the following code snippet. But first if I pass the assembly name and type to...
14
2276
by: z_learning_tester | last post by:
But I can't seem to find the answer. The question is how do you reverse the words in a string? Or how do you reverse the numbers listed in a string? The example is usually something like: Turn this string "1,2,3,4,..." Into "...4,3,2,1" This one seems hard enough let alone trying to turn a string of space-seperated words around(is that...
12
1868
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that the build fails with what the book tells me to do. Specifically, I am doing this: public authors1 GetAuthors() { authors1 authors = new...
5
2224
by: optimistx | last post by:
As a beginner in javascript I had a question. I was reading FAQ and posts here. I became very unhappy: Obviously this group is mainly for wise, pedantic, unkind etc people, who already know everything about javascript, and want to prove that to everyone in a very harsh way? Therefore, I skip the question, and we can go directly to the...
10
4432
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
1526
by: a | last post by:
Dear all vb.net developer I want to know the time I need to master vb.net? I'm beginner
3
2029
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but might have gotten the syntax wrong. the variable I want to write is a line from a file I am reading: "... f = open('receptor.mol2', 'r') line =...
2
1838
by: roanhn | last post by:
Hello. I've to to write a master's thesis. Currently I deal with php, mysql, ajax. Fate decreed that I've to choose one of this subjects: 1.gdi+ library in .net technology 2.ado.net technology in VS 2008. I didn't have contact with Visual Studio, .NET. I only know some basics of c++. My question is what subject of your point of view...
0
7542
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7467
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7736
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7500
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6066
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.