Solved

files

Posted on 2012-04-05
6
261 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 32

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now