Jump to content
grimx

c programming problem [solved]

Recommended Posts

I'm using Anjuta1.2.4

 

#include <stdio.h>

#define fSize 80

void Clear_Input(void);
void ReadFile(void);
void WriteFile(void);
void N_Crypt(void);
void D_Crypt(void);

int main(void)
{
char option;
int running = 1;

while(running)
{
	puts("===========Menu============");
	puts("1) ReadFile	2) WriteFile");
	puts("3) N_Crypt	 4) D_Crypt");
	puts("5) Quit");
	puts("===========================");

	option = getchar();
	if((option < '1') || (option > '5'))
	{
		Clear_Input();
		puts("Invalid Input");
		continue;
	}

	Clear_Input();

	switch(option)
	{
		case '1':
			ReadFile();
		break;
		case '2':
			puts("WriteFile");
		break;
		case '3':
			puts("N_Crypt");
		break;
		case '4':
			puts("D_Crypt");
		break;
		case '5':
			puts("GoodBye");
				running = 0;
		break;
		default:
				puts("Invalid Input");

	}

}

puts("Done");
return 0;
}

void Clear_Input(void)
{
while(getchar() != '\n')
	continue;
}

void ReadFile(void)
{
FILE *fp;
char fName[fSize];
int letter = 0;

printf("Enter File To Read: ");
fgets(fName, fSize, stdin);

printf("Opening %s ....\n", fName);
if((fp = fopen(fName, "r")) == NULL)
{
	printf("Unable To Open ( %s )\n", fName);
	return;
}

while((letter = getc(fp)) != EOF)
	putchar(letter);

fclose(fp);
printf("\nDone Reading Contents of ( %s )\n", fName);


}

void WriteFile(void)
{
}

void N_Crypt(void)
{
}

void D_Crypt(void)
{
}

 

my problem is in the ReadFile().

When i get a file name from the keyboard (say "Hello.txt") it does not open the file, but

when i hardcode the filename "Hello.txt", then it reads the file.

 

whats going on. :wall:

Edited by grimx

Share this post


Link to post
Share on other sites
The fgets function reads characters from the stream stream up to and including a newline character
So when you try to open the file, it looks for the filename "Hello.txt\n" which of course it can't find.

 

Yes, it's ugly :)

Share this post


Link to post
Share on other sites

I fixed it with

 

int size = 0;

size = strlen(fName);
fName[size - 1] = 'backslash 0';

 

problem solved..

Thanx again.

Edited by grimx

Share this post


Link to post
Share on other sites

Don't mean to intrude, since the problem is solved, but rather than this:

fName[size - 1] = 'backslash 0';

 

I would do this:

if(fName[size - 1] == '\n') 
fName[size - 1] = 'backslash 0';

 

so your code will be more portable.

Edited by Steve Scrimpshire

Share this post


Link to post
Share on other sites
I would do this:

if(fName[size - 1] == '\n') 
fName[size - 1] = 'backslash 0';

so your code will be more portable.

Wow, didn't think of portability! :) But I don't quite get that - what do you do if that character isn't \n, do nothing? Could you explain why?

Share this post


Link to post
Share on other sites

Right. Do nothing. You can actually make it an if - else if you wanted, but let's say you put this in a sub/function and called it from other places. This would ensure that you aren't stripping characters you don't want to strip.

 

In this particular case, it is perfectly safe to be used the way it is, because you know they have to hit Enter to get it to accept the input, so the '\n' will always be there, but if you wanted to make a function to strip '\n' for other purposes, you would need to make sure that's what you're stripping.

Share this post


Link to post
Share on other sites

Ah ok, I get it now. I thought when you said "portable" that you meant portable across systems, like win or mac, and thought it was something to do with the \n and the \r\n. Doh. But I get it now, that's a good idea. :thumbs:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...