Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Read from txt file

Reply
Thread Tools

Read from txt file

 
 
JrdA
Guest
Posts: n/a
 
      01-26-2004
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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-26-2004
"JrdA" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      01-26-2004
In article <401543ba$0$95068$(E-Mail Removed)> ,
JrdA <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      01-26-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-26-2004
"JrdA" <(E-Mail Removed)> wrote in message
news:401543ba$0$95068$(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting how often the same word appears in a txt file...But my codeonly prints the last line entry in the txt file dgcosgrave@gmail.com Python 8 12-19-2012 06:29 PM
Diff. between FileWriter("f.txt") and OutputStreamWriter(new FileOutputStream("f.txt")) ? Jochen Brenzlinger Java 7 09-15-2011 01:23 AM
Read .txt file like .py file King Python 2 07-28-2008 09:39 AM
Read xml file and write data to a txt file using PERL.... urgent Ram Perl Misc 3 04-26-2007 12:23 PM
Opening a txt file to view ( i.e. readme.txt) Sameen C++ 2 08-29-2005 03:14 PM



Advertisments