Reading string and integer file into array

April 30, 2011 at 13:56:19
Specs: Macintosh
I'm trying to read a file that contains integers and strings called club.txt that has the following data.

4
Ami
Ann
Ben
Dan
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0

My problem is reading the data into the appropriate variables. The first value (integer) 4 needs to be read into an integer size. This much I have figured out how to do, the rest is beyond me though.

I need to read the names Ami, Ann, Ben, Dan into a string array. Then read the following 1s and 0s into a 2D integer array. My biggest problem is that I have no way of counting which line is read. Any help?

This is what I have so far:

int get_data (int students_friends[][MAX_STUDENTS],string names[], int size)
{

//Local Declarations
int data_in;
int loader;
FILE* fp_data;

//Statements
loader = 0;

if ((fp_data = fopen ("clubs.txt", "r")) == NULL)
{
printf("Error opening file\a\a\n") ,
exit (100);
}

while (loader < size && fscanf(fp_data, "%d", &data_in) != EOF)
{
if(loader == 0)
{
size = data_in;
printf("%d", size);
loader++;

}
for (loader = 1; loader <= (size * 3) + 1; loader++)
names[loader - 1] = data_in;
break;
}
}


I'm also considering making names a char 2D array that reads each char individually.
This is for an intermediate C++ class so we havent covered vectors or anything confusing yet.

Thanks in advance !


See More: Reading string and integer file into array

Report •


#1
April 30, 2011 at 14:34:31
This is for an intermediate C++ class so we havent covered vectors or anything confusing yet.
Nor the standard file stream classes, apparently. Actually, why aren't you using ifstream?

As for the logic, you need to read the file in three separate places. The first for your size, the next for your names, and the last time for your digits.

I'm also considering making names a char 2D array that reads each char individually.
You're using an OOP language. Use objects (or at least structs).

How To Ask Questions The Smart Way


Report •

#2
April 30, 2011 at 14:53:40
I'm not sure why we aren't using ifstream. But this is the way my teacher wants it.

For the logic, how can I correctly read the file in 3 seperate places? More specifically, in the style my teacher prefers it?

I have also changed names to a 2D array. This is my new code that I've been working on; however, it only prints size, not the 2D array.

int get_data (int students_friends[][MAX_STUDENTS],
char names[][MAX_STUDENTS], int size)
{

//Local Declarations
int data_in;
int loader;
FILE* fp_data;

//Statements
loader = 0;

if ((fp_data = fopen ("clubs.txt", "r")) == NULL)
{
printf("Error opening file\a\a\n") ,
exit (100);
}

while (fscanf(fp_data, "%d", &data_in) != EOF)
{
if(loader == 0)
{
size = data_in;
printf("%d", size);
loader++;

}
if (loader == 1 && loader <= size * 3)
{
for (int r = 0; r < size; r++)
for (int c = 0; c < size; c++)
{
names[r][c] = data_in;
printf("%c", names[r][c]);
}
loader++;
break;
}
}
}


Report •

#3
April 30, 2011 at 16:16:52
how can I correctly read the file in 3 seperate places?
By having more than one fscanf(). Specifically, you'll need 3 of them, each with different format strings and arguments. As a bonus, you'll avoid that for-case anti-pattern you've got going on there.

The first one would probably look like this:

if (fscanf(fp_data, "%d", &size) != EOF && size > 0) {
You'll notice it isn't in a loop, but the two read loops you need will depend on this read.

Side note: you're passing in size by value. If you want a reference / pointer, you'll probably need to change that.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
April 30, 2011 at 17:44:07
Updated once again. Still running into the same issues, I'm guessing there is more than 1 problem. Any help?

int get_data (int students_friends[][MAX_STUDENTS],
char names[][MAX_STUDENTS], int size)
{

//Local Declarations
int data_in;
char name_data;
int loader;
FILE* fp_data;
char whitespace;
whitespace = NULL;

//Statements
loader = 0;

if ((fp_data = fopen ("clubs.txt", "r")) == NULL)
{
printf("Error opening file\a\a\n") ,
exit (100);
}

if (fscanf(fp_data, "%d", &data_in) != EOF && loader == 0)
{
size = data_in;
printf("%d", size);
loader++;
printf("\n%d", loader);
}

if (fscanf(fp_data, "%c", &name_data) != EOF && loader > 0 && loader <= 12
&& name_data != whitespace)
{
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
{
names[row][col] = name_data;
printf("%c", names[row][col]);
}
}
}
}


Report •

#5
April 30, 2011 at 19:32:44
Off the top of my head, you should probably be using %s instead of %c.

And your second read is an if statement, not a while loop.

And you check to see if loader <= 12, but the code path ensures it's either 0 or 1.

And you're never using size, which is required to know when to stop looking for names, and to start looking for (I assume) attributes.

How To Ask Questions The Smart Way


Report •


Ask Question