?
Solved

How to read in newline or null-terminated words into a cstring array

Posted on 2005-03-06
5
Medium Priority
?
333 Views
Last Modified: 2012-08-13
Hello, I was wondering what would be a better technique to read in a file of newline-terminated words and place them into an array of cstrings.  Do I have to read in each word char by char?

E.g.
The text file is as such:

car
apple
cities
ladies
Corvette


 Say I create a struct to hold the char array:

struct HoldTheChars
{
   char chArray[30];
};

...and I create an array of Type "HoldTheChars"

HoldTheChars strArray[45];

... and then use a double for-loop to fill in the array.
Is this the most efficient way?

Basically, I'd like to read in each word, as a cstring, and place it in an array, for future tests on how to search, binary or hashing.

0
Comment
Question by:VDOGamer
[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
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 13471089
you could do

ifstream F

while(!F.feof())
  f.getline(Buffer[i++], 200);


something along thoselines
0
 
LVL 6

Expert Comment

by:HyperBPP
ID: 13473626
#include <stdio.h>
#include <string>
#include <iostream.h>
#include <fstream.h>


void main()
{      
      //Variables
      std::string str[100];
      char temp[1024];
      int i=0;
      ifstream fin;

      //Open up a file called test.txt for reading
      fin.open("test.txt", ios::in);


      //Read test.txt until end of file has been reached
      //Reads a line until 1024 characters have been read
      //or until the newline character has been read.  Then
      //stores that word (or words) in a string.
      while(!fin.eof())
      {
            fin.getline(temp, 1024, '\n');
            str[i++]=temp;
      }

      return;
}
0
 
LVL 6

Expert Comment

by:HyperBPP
ID: 13473697
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>


void main()
{      
      //Variables
      char str[100][100];
      int i=0;
      ifstream fin;

      //Open up a file called test.txt for reading
      fin.open("test.txt", ios::in);


      //Read test.txt until end of file has been reached
      //Reads a line until 100 characters have been read
      //or until the newline character has been read.
      while(!fin.eof())
      {
            fin.getline(str[i++], 100, '\n');
            printf("%s", str[i-1]);
      }

      return;
}
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 13485133
All sample code above don't handle last line correctly. The last getline call most likely fails but none of these samples handle the fail condition. Try that:

#include <string>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
     string line;
     vector<string> strArr;
     ifstream ifs("text.txt");
     while (getline(ifs, line))
          strArr.push_back(line);

     return 0;
}

You also could use your definitions but std::string and std::vector would be the better choice. Also, you should add a constructor to properly initialize your struct.

struct HoldTheChars
{
   char chArray[30];
   HoldTheChars() { for (int i = 0; i < sizeof(chArray); ++i) chArray[i] = '\0'; }
};

#include <string>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
     HoldTheChars strArray[45];
     ifstream ifs("text.txt");
     int i = 0;
     while (i < sizeof(strArray)/sizeof(HoldTheChars) && 
               ifs.getline(strArray[i], sizeof(HoldTheChars)) )
           i++;
     ifs.close();
     return i;
}

Regards, Alex





0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 13485162
I forgot a

    ifs.close()

in the first sample. In the second sample I need to access the struct member

    while (i < sizeof(strArray)/sizeof(HoldTheChars) && 
               ifs.getline(strArray[i].chArray, sizeof(HoldTheChars)) )
    ..

Regards, Alex



   
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

765 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