Solved

files

Posted on 2012-04-05
6
270 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 34

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory going from 12gb to 64gb or 96gb. worth it? 15 213
Query group by data in SQL Server - cursor? 3 64
backup program with robocopy 6 69
Ruby or Python 7 42
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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…

685 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