Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C Program to read from another file and print to screen

  • 10-11-2011 1:47pm
    #1
    Closed Accounts Posts: 95 ✭✭


    I am writing a C program to ask a user for the file they want to see the text from. Then the program reads the file and prints to the screen.

    I've written this but whilst it compiles the program will only ask for the file, then when this is input it will do nothing, the file I'm trying to open exists (I'm trying it with a file called TextFile.txt which I've saved in the same area as this program). Any advice would be gratefully received.
    #include<stdio.h> 
    int main()
    {
    	FILE *fopen(), *fp, *fprint;
    	int letter;
    	char fname[80];
    	printf("Please enter the name of the file you wish to open: ");
    	gets(fname);
    	fp=fopen (fname, "r");
    	letter = getchar();
    		while (letter != EOF)
    		{
    		putchar(letter);
    		letter=getchar();
    		}
    return 0;
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    You should use char for the letter not int. A char is byte, a integer is 4 bytes normally.

    Without looking into it too far I think this may be a problem.


  • Registered Users, Registered Users 2 Posts: 365 ✭✭jayo99


    why have you got this line -

    FILE *fopen(), *fp, *fprint; ?


    remove *fopen(), as fopen() is a method name

    FILE *fp, *fprint;


  • Closed Accounts Posts: 95 ✭✭The Crab


    Thanks both. I did both of those things but I still have the same problem.

    I'm completely new to this so I'm a bit confused, my lecturer said "letter" had to be an int because the EOF thing is -1 so a char won't work with it?

    Annoying thing is I have FOUR programs like this which I need to submit, but the four won't work cos I'm doing something wrong and can't work out what lol.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Sorry yes you are correct. So what happens. Have you set break points? Step throguh the code and see what's happening.

    Edit: Just noticed. Getchar is getting a character from STDIN (keyboard) and not the file.

    Use
    fgetc(fp);
    

    Instead


  • Closed Accounts Posts: 95 ✭✭The Crab


    Webmonkey wrote: »
    Sorry yes you are correct. So what happens. Have you set break points? Step throguh the code and see what's happening.

    Edit: Just noticed. Getchar is getting a character from STDIN (keyboard) and not the file.

    Use
    fgetc(fp);
    

    Instead

    Perfect, thanks! I'd have been working at that for hours and wouldn't have known.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,534 ✭✭✭FruitLover


    FYI, a char should work for 'letter' as long as it's signed. Whether a char is signed by default or not is dependent on implementation, but usually it's signed (force by declaring 'signed char' just in case).


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    FruitLover wrote: »
    FYI, a char should work for 'letter' as long as it's signed. Whether a char is signed by default or not is dependent on implementation, but usually it's signed (force by declaring 'signed char' just in case).
    255 would be the -1 then in this case but what if you want the character 255 "ÿ". Unlikely I know :)


Advertisement