I have a while loop reading names from a file and placing them in an array. When I print the contents of the array, it prints the last name that went through the loop, so I'm assuming that as the loop is progressing each time the names are being rewritten. I have attached my code, if you have any idea of what's wrong could you let me know please? My code is below.

if ((fp= fopen(filename, "r"))==NULL)
	{
		printf("Can't open %s.\n", filename);
		return 1;
	}

while ((fscanf(fp,"%s %s %c", last, first, &middle)) !=EOF) 
	{ 
	k++;
	printf("%s %s %c\n", last, first, middle);
	sprintf(student_file, "%s %s %c", last, first, middle);
	}

for( i=0; i<k; i++)
{
	printf("%s\n", student_file);
}

I'm assuming that 'student_file' is an array of strings?

Do these changes help at all? 11. sprintf(student_file[k], student_file, "%s %s %c", last, first, middle); 16. printf("%s\n", student_file[i]); If they don't help, can you post your declaration of 'student_file'?

No that did not help, I'm assuming that it is an array of strings, though I'm having trouble doing that. Here is my complete code:

# include <stdio.h>
# include <string.h>

int main()
{
	FILE *fp, *student;
	char filename [30], student_file [30];
	char first[30], last[30], middle;
	int i, sum, k=0, num;

	printf("Enter a filename:\n");
	scanf("%s", &filename);

	if ((fp= fopen(filename, "r"))==NULL)
	{
		printf("Can't open %s.\n", filename);
		return 1;
	}

while ((fscanf(fp,"%s %s %c", last, first, &middle)) !=EOF) 
	{ 
	k++;
	printf("%s %s %c\n", last, first, middle);
	sprintf(student_file, "%s %s %c", last, first, middle);
	}

for( i=0; i<k; i++)
{
	printf("%s\n", student_file);
}

fclose(fp);
return 0;
}

Well, the 61 characters you are reading into first, last, and middle are being loaded into the 30 character array student_file (do you see a problem there?)

Then next read loads the next name into the 30 character array student_file (what happened to the previous name loaded?)

Also, are you sure first, last, and middle get the data you read correctly? You might want to print them out to verify it.

Yes, I see that the data is overwritten, I do not know how to stop that from happening. and I tried making a 90 character array, however, I'm not actually reading in 30 characters, it's only just in case there happens to be 30 characters, but changing the other array to 90 didn't do anything.

I have placed a print statement into the while loop to see whether the actual data is being read, and yes it is.

Ah! OK. Try these changes, then. 7. char filename [30], student_file [30][30]; 24. sprintf(student_file[k], student_file, "%s %s %c", last, first, middle); 29. printf("%s\n", student_file[i]); You've created 'student_file' as an array of 30 characters. This declaration creates it as an array of 30 strings of 30 characters each. In lines 24 and 29, we access the kth and the ith strings in that array respectively as we go through the while loop.

Hopefully this does the trick.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.