473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Read from txt file

Hi NG

My programs does following:

- ofstream writes fx. "1234#01/26/04#" to a txt-file

Now i have a txt-file containing :

1234#01/26/04#
424#01/26/04#
324234#01/26/04#

I then need ifstream to read the first ints until it reaches the # sign and
then add them togheter.

Can anyone help me with som code to do that?

Thanks
AHR
Jul 22 '05 #1
4 2353
"JrdA" <n@n.n> wrote...
My programs does following:

- ofstream writes fx. "1234#01/26/04#" to a txt-file

Now i have a txt-file containing :

1234#01/26/04#
424#01/26/04#
324234#01/26/04#

I then need ifstream to read the first ints until it reaches the # sign and then add them togheter.

Can anyone help me with som code to do that?


The usual approach is to read the entire line into a string object,
then extract the "field" you need (in your case all symbols until
the first '#' into a separate string) and then work with it (in your
case convert it into an int).

There are other solutions, but this one is the most generic.

To read an entire line use 'std::getline' function. To extract the
substring use 'substr' member of std::string. To convert a string
into a number use 'std::istringstream' and its operator >>.

Victor
Jul 22 '05 #2
In article <40***********************@dread11.news.tele.dk> ,
JrdA <n@n.n> wrote:

Now i have a txt-file containing :

1234#01/26/04#
424#01/26/04#
324234#01/26/04#

I then need ifstream to read the first ints until it reaches the # sign and
then add them togheter.


I assume you don't need to do anything with the rest of the data on each
line.

When you read an int using >>, it will stop reading when it reaches the
first character that cannot be part of an int, so it will stop at the
first # sign. Now now you need to skip over the rest of the line, which
you can do with ignore().

ifstream input ("your.file");
int num;
while (input >> num)
{
// do whatever you need to do with num here

input.ignore (1000, '\n'); // skips past next newline, or 1000 chars,
// whichever comes first
}

Depending on the maximum posslbie line length in your file, adjust the
1000 if necessary.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3
JrdA wrote:

Hi NG

My programs does following:

- ofstream writes fx. "1234#01/26/04#" to a txt-file

Now i have a txt-file containing :

1234#01/26/04#
424#01/26/04#
324234#01/26/04#

I then need ifstream to read the first ints until it reaches the # sign and
then add them togheter.

Can anyone help me with som code to do that?


I would do it the following way:

read an entire line into a std::string using getline
then search if and where there is a '#' character.
Split the string into 2 using the found position
Use the left half in a stringstream to use an op>> to read the int.

Now that you have that int, use it for whatever you like to do with it.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
"JrdA" <n@n.n> wrote in message
news:40***********************@dread11.news.tele.d k...
Hi NG

My programs does following:

- ofstream writes fx. "1234#01/26/04#" to a txt-file

Now i have a txt-file containing :

1234#01/26/04#
424#01/26/04#
324234#01/26/04#

I then need ifstream to read the first ints until it reaches the # sign and then add them togheter.

Can anyone help me with som code to do that?


#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::ifstream input("filename");
if(!input)
{
std::cerr << "Cannot open input\n";
return EXIT_FAILURE;
}

int sum(0);
std::string line;
const std::streamsize width(10);
std::streamsize line_no(0);
bool err(false);

while(std::getline(input, line))
{
++line_no;
int value(0);
std::istringstream iss(line);

if(!(iss >> value))
{
std::cerr << "Line " << line_no << ": "
<< "First field not a valid integer, skipped\n";

err = true;
}
else
{
std::cout << " + " << std::setw(width) << value << '\n';
sum += value;
}
}

std::cout << " " << std::string(width, '-') << '\n'
<< " = " << std::setw(width) << sum << '\n';

if(!input && !input.eof() || err)
std::cerr << "(Error(s) occurred reading input)\n";

return 0;
}

-Mike
Jul 22 '05 #5

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.