Read from txt file

J

JrdA

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
 
V

Victor Bazarov

JrdA said:
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
 
J

Jon Bell

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.
 
K

Karl Heinz Buchegger

JrdA said:
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.
 
M

Mike Wahler

JrdA said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top