Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

files

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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
Problem to open text file 11 128
Android development question 2 54
Cygwin - GNU GPL License 1 37
AvlTree-Node Data type 4 14
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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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