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 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
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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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...
|
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...
|
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...
|
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...
| |
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
....
|
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
|
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 =...
|
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...
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |