954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

File in-out not printing or creating out file

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);
					}
			}
}
Soup!
Newbie Poster
1 post since Dec 2010
Reputation Points: 25
Solved Threads: 0
 

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:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

jnawrocki
Light Poster
40 posts since Aug 2010
Reputation Points: 7
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You