Re: Read from txt file
From: Jon Bell (jtbellj3p_at_presby.edu)
Date: 01/26/04
- Next message: Ron Hardin: "Re: Parsing English with lex and yacc"
- Previous message: Victor Bazarov: "Re: Read from txt file"
- In reply to: JrdA: "Read from txt file"
- Next in thread: Karl Heinz Buchegger: "Re: Read from txt file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 26 Jan 2004 16:51:41 +0000 (UTC)
In article <401543ba$0$95068$edfadb0f@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 <jtbellm4h@presby.edu> Presbyterian College Dept. of Physics and Computer Science Clinton, South Carolina USA
- Next message: Ron Hardin: "Re: Parsing English with lex and yacc"
- Previous message: Victor Bazarov: "Re: Read from txt file"
- In reply to: JrdA: "Read from txt file"
- Next in thread: Karl Heinz Buchegger: "Re: Read from txt file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|