LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need C++ Program to Read from Text File (https://www.linuxquestions.org/questions/programming-9/need-c-program-to-read-from-text-file-594839/)

waXed 10-26-2007 12:57 PM

Need C++ Program to Read from Text File
 
The basic idea is that I have a text file with lines of data.

The text file will read lines like:
fNameone
ele
ele
ele
lee
lee
lee
lee
eel
fNametwo
ele
ele
ele
lee
lee
lee
lee
eel
eel
eel
eel
eel
eel



it will continue on many different names, it will know when a new name is reached by the first character being f. each time an f line is hit, then a counter variable c needs to increase by 1 to keep track of how many names and assign the name, excluding the f to name(c) until the next f line is hit, then it will count the number of items (ele's, lee's, eel's) contained by each name and assign them to a variable (ele(c), lee(c), eel(c) respectively.)

not sure this will make sense, and if it doesn't please let me know and i will try to explain it in a different way.

matthewg42 10-26-2007 01:05 PM

I will do your homework if you buy me one years worth of beer. You deliver first.

P.S. That is quite a lot of beer, and I have expensive tastes.

matthewg42 10-26-2007 01:07 PM

Seriously, what do you think this is here? Read the forum rules. We are not going to do youor homework for you. It won't help you either - you will not understand it if you cannot do it yourself.

We will help point you in the right direction, but you have to have a go first and ask specific questions. If you have no clue how to write a program in C++ - no idea even where to start, then you need to talk to your course tutor because you have not learned anything.

waXed 10-26-2007 01:08 PM

Quote:

Originally Posted by matthewg42 (Post 2937923)
I will do your homework if you buy me one years worth of beer. You deliver first.

P.S. That is quite a lot of beer, and I have expensive tastes.

this isn't homework, it's just a minor piece of a project i am trying to make for a friend.. but i'm still new to c++ and don't really know much of the textfile related stuff.

waXed 10-26-2007 01:15 PM

Quote:

Originally Posted by matthewg42 (Post 2937926)
Seriously, what do you think this is here? Read the forum rules. We are not going to do youor homework for you. It won't help you either - you will not understand it if you cannot do it yourself.

We will help point you in the right direction, but you have to have a go first and ask specific questions. If you have no clue how to write a program in C++ - no idea even where to start, then you need to talk to your course tutor because you have not learned anything.

how about this.

text file contains:


fSectionone
ele
lee
eel
fSectiontwo
ele
lee
eel



i need to know how to pull each line from the text file, and what function finds the first character of the string to determine if it is an f line.

matthewg42 10-26-2007 01:21 PM

This sort of task is really much easier in awk or Perl.
For example, if I understand your description properly, this Perl program will do what you want:
Code:

#!/usr/bin/perl

$current_name = "NONAME";

while(<>) {
        if ( /^f(.*)$/ ) {
                $current_name = $1;
        }
        else {
                $count{$current_name}++;
        }
}

foreach $key (keys %count) {
        printf "%-20s %d\n", $key, $count{$key};
}

Just save this into a file called "myprogram", change the permission so it is executable using this command:
Code:

chmod 755 myprogram
And then run it like this:
Code:

./myprogram input_file
The output will look something like this:
Code:

Nametwo              13
Nameone              8



All times are GMT -5. The time now is 08:37 AM.