Jump to content

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
Link to comment
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
Link to comment
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?

Link to comment
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.

Link to comment
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...
 Share

×
×
  • Create New...