Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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

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
VDOGamer
Asked:
VDOGamer
  • 2
  • 2
1 Solution
 
Jase-CoderCommented:
you could do

ifstream F

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


something along thoselines
0
 
HyperBPPCommented:
#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
 
HyperBPPCommented:
#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
 
itsmeandnobodyelseCommented:
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
 
itsmeandnobodyelseCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now