Solved

files

Posted on 2012-04-05
6
266 Views
Last Modified: 2012-08-02
Hi

I am reading from a file and would like to store only the last word of each line in an array.

for example:

This is line one\n
This is line two\n

I would like to store the word one and then the word two, disregarding the other words.

Can anyone offer any help, Im using c?

Cheers
0
Comment
Question by:codey-06
6 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37810431
A pure bash/awk solution:

myarray=( $(awk '{print $NF}' inputfile) )

To display the new array:

echo ${myarray[*]}
0
 

Author Comment

by:codey-06
ID: 37810491
Do you know how I could implement this in a c program file?  I would also like to store one and two in 2 separate char arrays?

Thank you
0
 
LVL 33

Expert Comment

by:sarabande
ID: 37811403
read line by line calling fgets into a char buffer, right-trim the buffer (also strip linefeed character) and then search for space char most right calling strrchr. increment the pointer you got returned from strrchr and make a copy for example by passing it to strdup. you then would add that pointer to an array of char* or to a linked list.

Sara
0
 
LVL 9

Accepted Solution

by:
Orcbighter earned 500 total points
ID: 37950569
It is quite easy, using the stream classes,
The following code opens the file for input, then in a read-loop, reads in each line of the file.
For each line it uses the istringstream class to break up the line into each word until the end of line (iss == NULL). for each word, if the value is not null, it saves it to the lastWord variable.
If, after the loop, that variable contains a value, it will be the last word of the line just retrieved.


#include <tchar.h>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>




int _tmain(int argc, _TCHAR* argv[])
{
    bool bAll_OK = true;
    bool bFileOpen = false;
    string inFilename = L"C:\\Temp\\MyFile.txt";
    fstream infile;

    try
    {
        infile.open( inFilename.c_str(), ios_base::in );
        infile.exceptions(  fstream::eofbit | fstream::failbit | fstream::badbit);

        if ( infile.is_open() )
        {
            bFileOpen = true;
        }
    }
    catch( fstream::failure er )
    {
        Cerr << er.what() << endl;
        bAll_OK = false;
    }

    // file is open, lets read it.
    while ( bAll_OK )
    {
        try
        {
            string tmpBuffer, lastWord;
            istream& ist = infile;

            getline( ist, tmpBuffer );
            istringstream iss( tmpBuffer );
   
            do
            {
                string subStr;
                iss >> subStr;

                if ( subStr.length() > 0 )
                {
                    lastWord.assign( subStr );
                }
            } while ( iss != NULL );

            if ( lastWord.length() > 0 )
            {
                // add code here to store the last word
            }
        }
        catch( fstream::failure er )
        {
            cerr << er.what() << endl;
            bAll_OK = false;
        }
    }

    return 0;
}
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 38249580
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Resolve Dependency Issues 4 64
Android development question 2 35
object oriented design (python) and documentation tools 2 36
Test the speeds on my PC Drives 12 43
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question