Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

files

Posted on 2012-04-05
6
Medium Priority
?
277 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
[X]
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
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 35

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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

719 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