Hello, I'm new with C and I've almost finished this little program but something is wrong.
The program reads in a list of names from the file names.txt that I put right in C:, and prints the list to an out file called trans.txt (which it creates on its own) and prints the list to the screen. It executes but only prints the user instructions, and not the names.
I even searched all of C: for trans.txt and there is no sign of it, even though I told the program to create it directly in C:. I am not sure what to do or what is happening, any help would be appreciated.

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

char names[20][60];
int i, j;
char name;

void main(void)
{
FILE *in, *out; /* access to in.txt files */
printf("This program will read in names from the \nfile names.txt and print out to trans.txt.\n");

in = fopen("c:\\names.txt", "r");
out = fopen("c:\\trans.txt", "w");

for (i=0; i<20; i++)

	for(i=0;i<60;i++)
		names[i][j]=' ';

			for (i=0;i<20; i++)
			{
			 fgets(&names[i][j], sizeof names[i][j], in);
				if (names[i][j]=='\0' || names[i][j]=='\n')
					 printf (" ");
				else
					{
					 names[i][j]=names[j][i];
					 printf("%c", names);
					}
			}
}
WaltP commented: Using CODE Tags first time!!!! What's wrong with the rest of the world? +15

Recommended Answers

All 2 Replies

Please see this to format your code so we can read it (it's very hard to follow as it is) and also this about main().

What is the value of sizeof names[i][j] ? Are you sure this is what you want?


It really helps to print the value of important variables as the program runs to make sure they are the values you expect.
1) If they aren't what you expect, figure out why.
2) If you don't know what to expect, sign up for shop :icon_mrgreen:

I'm not sure but it looks like all your for loops are using "i" as an index. Maybe you should use i1,i2,i3 or something.You are also using a secondary index j but I do not see where j is initialized. Finally I think your file problem might stem from c:\\. If you want to put the file in the root directory I think it should be c:\trans.txt if you want to put in the currrent directory just names.txt or c:.\names.txt.

Hope this helps a little.

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.