Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading from file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi! I hope u can help me cause I'm kinda out of ideas.
I want to read from a file some names and store it into an array.

The problem is that the code below reads the whole line. All I want it to do is read the names.

----------THE FILE IT'S READING FROM LOOKS SOMETHING LIKE---
Name 34783
Name2 452894
Name3 3474
Name4 34111323
-------------------------------------------------------

I'm new to this and only know the basics. Keep it simple if u can as it would help alot.


#include <iostream.h>
#include <string.h>
#include <fstream.h>
#define size 10

class List_class
{
private:
char names[size][20];

public:
List_class();
void to_screen ();
};


void main(void){
List_class num_list;

num_list.to_screen ();
} // main

List_class::List_class()
{
fstream infile (&quot;Names.dat&quot;, ios::in);
for (int a = 0; a < size; a++){
infile >> names[a];

infile.close();
}


void List_class::to_screen ()

{cout << &quot;This is the list: &quot; << endl << endl;
for (int a = 0; a < size; a++)
cout << names[a] << &quot; &quot;;
}




Thanks guys for the help if you possible. It's greatly appreciated. Thanks a million :)
 
You have two problems that I can easily see. Both deal with your character array names. The first is the input code:
Code:
for (int a = 0; a < size; a++){
infile >> names[a];
and the second is the output:
Code:
for (int a = 0; a < size; a++)
cout << names[a] << &quot; &quot;;

Aside from missing a } on the input, you've defined names as a two dimensional array (10 x 20) while in both places you are using a single array. Even if you defined it as a single array, your input would create a buffer overrun since you've allocated only 10 places and your input file is bigger than that. With a two dimensional array, your output could look like:
Code:
for (int a = 0; a < 20; a++)
{
  for (int b = 0; b < 9; b++)
  {
    // This will print out the first 9 characters
    // the 10th is a termination character which is
    // not needed with cout
    cout << names[b][a];
  }
  cout << &quot; &quot;;
}
cout << endl; // This flushes the cout buffer and does
              // a cr/lf.

I'll leave the input for you. Personally, I prefer strings over character arrays. One, they are easier to use, IMHO, than character arrays; two, they don't allow buffer overruns; and three, in this case, they would eliminate the dual for loops.

BTW, I'm doing this from memory so watch out for bugs that come from my poor memory. James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Can I make it stop reading and storing into the 2 dimensional array above once it encounters a 'WHITESPACE' in the file.

So basically I just want it to store the names only and not the number. This this possible and how do i go about doing this?



Many thanks in advance. Greatly appreciated! :)
 
whitespace is a '\0'. have it read the line until it hits a '\0' and then stop. then take what it's read so far and that will be the name. I will get some code up for you soon if you cant figure it out. Cyprus
 
#include <iostream.h>
#include <string.h>
#include <fstream.h>
#define size 10

class List_class
{
private:
char names[size][20];

public:
List_class();
void to_screen ();
};

List_class::List_class()
{
fstream infile (&quot;Names.dat&quot;, ios::in);
for (int a = 0; a < size || !infile.eof(); a++)
{
infile.getline(names[a], 20);
}
infile.close();
}


void List_class::to_screen ()

{
cout << &quot;This is the list: &quot; << endl << endl;
for (int a = 0; a < size; a++)
cout << names[a] << endl;
}

int main(void)
{
List_class num_list;

num_list.to_screen ();
}

 
Why not just use TIniFile? [pc3]
In the end it will probably be the simplest, most obvious thing that I was doing wrong...

[smarty] [tt]One is glad to be of service
- Bicentennial man[/red][/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top