LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2007, 01:57 PM   #1
waXed
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Rep: Reputation: 0
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.
 
Old 10-26-2007, 02:05 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 10-26-2007, 02:07 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 10-26-2007, 02:08 PM   #4
waXed
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 10-26-2007, 02:15 PM   #5
waXed
LQ Newbie
 
Registered: Oct 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 10-26-2007, 02:21 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 10:50 AM
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 08:29 AM
most optimal way to read a text file that is gzip'ed in C? hedpe Programming 2 06-27-2006 11:46 AM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 04:03 PM
How to read and change the contents of a text file? Bassam Linux - General 11 02-26-2004 05:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration