Can anyone help? New programmer trying to write Program that prompts user to enter name of file to display on screen.
User is given to option to display another file until the user ends program. it is not working and. After compilation when 1 is chosen it scroll the question enter name of the text file name to display. when 0 is chosen the program does the same.

Many thanks

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

#define BUFSIZE 100

main()
{
	char buf[BUFSIZE];
	char filename[60];
	FILE *fp;
	int reply;
		
	
	
		printf( "Enter 1 to select file to display 0 to exit " );
		scanf("%d", &reply);
	
		while( reply = 1 )
			puts( "Enter name of text file to display: " );
			gets(filename);
		
			
	
		/* Open the file for reading. */
		if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		/* If end of file not reached, read a line and display it.*/
	
		while( !feof(fp) )
			{
				fgets( buf, BUFSIZE, fp);
				printf( "%s", buf );
			}
	
		fclose(fp);
		
		
		return (0);
}

Recommended Answers

All 10 Replies

Can anyone help? New programmer trying to write Program that prompts user to enter name of file to display on screen.
User is given to option to display another file until the user ends program. it is not working and. After compilation when 1 is chosen it scroll the question enter name of the text file name to display. when 0 is chosen the program does the same.

Lots wrong... while( reply = 1 ) -- This sets reply to 1 all the time. Did you mean == for a comparison? gets(filename); -- See This while( !feof(fp) ) -- See This

And your formatting can use some help, too.

Welcome to the Forum, 1150! ;)

Scanf() leaves a newline in the keyboard buffer, and it's throwing a monkey wrench into gets().

The fix is to add this, right after the scanf() line of code:

getchar();  //pulls off one (newline in this case), char.

Thanks adak but the program is not working after making the changes you recommended. Below the amended code. Can someone help? When I compile and choose 1 to view file Enter name of file to display is displayed but on entering a named file nothing is displaced except an empty line. Pressing enter only displays the message Error opening file. Is this program possible the way I want it.

Thanks much.

/* Programs prompts user to enter name of file to display on screen */
/* user is given to option to display another file until the user ends */
/* program */

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

#define BUFSIZE 100

main()
{
	char buf[BUFSIZE];
	char filename[60];
	char dummy[50];
	FILE *st;
	int reply;
		
	printf( "Enter 1 to select file to display 0 to exit " );
	scanf("%d", &reply);
	getchar();  //pulls off one (newline in this case), char.
	
	while( reply == 1 )
	{
		puts( "Enter name of file to display: " );
			
		fgets(filename, 60, stdin);  // 60 is the size of filename 
	
		if (filename[strlen(filename)-1] == '\n') 
		{	
			// full input line read 
    			filename[strlen(filename)-1] = '\0';  // remove the new-line
		}
		else
			{	// parial input line read
				filename[0] = 0;  // empty the filename buffer
			}
    		do
    		{	// loop until the new-line is read
        		fgets(dummy, 50, stdin);
        		strcat(filename, dummy);  // Save input but be sure
        					// sure to test your buffer size
		} while (dummy[strlen(dummy)-1] != '\n');

		// Open the file for reading.
		if ( ( st = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		// If end of file not reached, read a line and display it.
	
		while (fgets(buf, BUFSIZE, st) != NULL)
		{
		   // process your buffer
		    puts(buf);
		}
		fclose(st);
		return (0);
	}
}

Can anyone help? New programmer trying to write Program that prompts user to enter name of file to display on screen.
User is given to option to display another file until the user ends program. it is not working and. After compilation when 1 is chosen it scroll the question enter name of the text file name to display. when 0 is chosen the program does the same.

Many thanks

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

#define BUFSIZE 100

main()
{
	char buf[BUFSIZE];
	char filename[60];
	FILE *fp;
	int reply;
		
	
	
		printf( "Enter 1 to select file to display 0 to exit " );
		scanf("%d", &reply);
	
		while( reply = 1 )
			puts( "Enter name of text file to display: " );
			gets(filename);
		
			
	
		/* Open the file for reading. */
		if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		/* If end of file not reached, read a line and display it.*/
	
		while( !feof(fp) )
			{
				fgets( buf, BUFSIZE, fp);
				printf( "%s", buf );
			}
	
		fclose(fp);
		
		
		return (0);
}

In this part of the code, the do while loop, belongs INSIDE the else statement, not after the else statements closing brace.

else
			{	// parial input line read
				filename[0] = 0;  // empty the filename buffer
			[B]}[/B]
                /* this do while loop, belongs INSIDE your else block, not outside
    		do
    		{	// loop until the new-line is read
        		fgets(dummy, 50, stdin);
        		strcat(filename, dummy);  // Save input but be sure
        					// sure to test your buffer size
		} while (dummy[strlen(dummy)-1] != '\n');

Also:

your reply query is still outside the largest while loop, and needs to be brought inside it. Unless that is done, you won't see that query again.

You are trying to read from the file before you open it. Open it first, then read.

If the line of text is longer than 60 chars, do you really want to remove it?

Thanks adka for your advice. I made the changes your requested and it worked as promised. Later I tried to get the program to run repeatedly without having to recompile. I moved the query reply to the largest while block but that did not work. I tried changing the while to a do while block but it still doesn't work. when compiled and 1 is selected it just displays the same query. Selecting 0 causes the program to display "Enter name of file to display: . The program does not repeat the query and terminates.

Can you help me again?

The line of text is limited to 60 char because I did not think a filename would be longer. If it could, please advise.

Below see copy of code. Many thanks.

/* Programs prompts user to enter name of file to display on screen */
/* user is given to option to display another file until the user ends */
/* program */

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

#define BUFSIZE 100

main()
{
    char buf[BUFSIZE];
    char filename[60];
    char dummy[50];
    FILE *st;
    int reply;

    do
    {
        printf( "Enter 1 to select file to display 0 to exit " );
        scanf("%d", &reply);    

        getchar();  //pulls off one (newline in this case), char.
    }while( reply == 1 );

        puts( "Enter name of file to display: " );  
        fgets(filename, 60, stdin);  // 60 is the size of filename 

        if (filename[strlen(filename)-1] == '\n') 
        {   
            // full input line read 
                filename[strlen(filename)-1] = '\0';  // remove the new-line
        }
        else
            {   // partial input line read
                filename[0] = 0;  // empty the filename buffer

            do
            {   // loop until the new-line is read
                fgets(dummy, 50, stdin);
                strcat(filename, dummy);  // Save input but be sure
                                    // sure to test your buffer size
        } while (dummy[strlen(dummy)-1] != '\n');

            }

        // Open the file for reading.
        if ( ( st = fopen(filename, "r")) == NULL )
            {
                fprintf( stderr, "Error opening file.\n" );
                exit(1);
            }

        // If end of file not reached, read a line and display it.

        while (fgets(buf, BUFSIZE, st) != NULL)
        {
            // process your buffer
            puts(buf);
        }

        fclose(st);     
        return (0);
    }

I am using a new browser midori hope code lines up if not I will revert to firefox.

Can anyone help? New programmer trying to write Program that prompts user to enter name of file to display on screen.
User is given to option to display another file until the user ends program. it is not working and. After compilation when 1 is chosen it scroll the question enter name of the text file name to display. when 0 is chosen the program does the same.

Many thanks

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

#define BUFSIZE 100

main()
{
    char buf[BUFSIZE];
    char filename[60];
    FILE *fp;
    int reply;



        printf( "Enter 1 to select file to display 0 to exit " );
        scanf("%d", &reply);

        while( reply = 1 )
            puts( "Enter name of text file to display: " );
            gets(filename);



        /* Open the file for reading. */
        if ( ( fp = fopen(filename, "r"))  == NULL )
            {
                fprintf( stderr, "Error opening file.\n" );
                exit(1);
            }

        /* If end of file not reached, read a line and display it.*/

        while( !feof(fp) )
            {
                fgets( buf, BUFSIZE, fp);
                printf( "%s", buf );
            }

        fclose(fp);


        return (0);
}

I would change this code:

printf( "Enter 1 to select file to display 0 to exit " );
		scanf("%d", &reply);
	
		while( reply = 1 )
			puts( "Enter name of text file to display: " );
			gets(filename);
		
			
	
		/* Open the file for reading. */
		if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		/* If end of file not reached, read a line and display it.*/
	
		while( !feof(fp) )
			{
				fgets( buf, BUFSIZE, fp);
				printf( "%s", buf );
			}
	
		fclose(fp);

to this:

for(i=0;i<sizeof(buff);i++) {
                   buff[i] = '\0';  
                } 
                reply = 1;	
		while( reply == 1 ) {
	           printf( "Enter 1 to select file to display 0 to exit " );
		   scanf("%d", &reply);
                   getchar();
                   if(reply == 0)
                      continue; //loop again, effectively breaking out if reply is 0
 
		   puts( "Enter name of text file to display: " );
		   fgets(filename, sizeof(filename), stdin);
                   if(filename[strlen(filename)-1]== '\n'
                      filename[strlen(filename)-1] = '\0'; //remove newline from string
                     
		   /* change any backslash char's to forward slash, in buffer */
                   int len = strlen(filename); 
                   for(i=0;i<len;i++) {
                      if(filename[i] == '\') {
                         filename[i] = '/';
                      }
                   }   	           
                		
		   // Open the file for reading. 
		   if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		   // If end of file not reached, read a line and display it.
	
		   while((fgets( buf, BUFSIZE, fp)) != NULL) {
			printf( "%s", buf );
		   }
	
		   fclose(fp);
                } //end of while(reply == 1)

I haven't run this, so it may not be perfect, but give it a shot. I'm not certain how long a file name can be. Check with it's help or website help.

Adak thanks for the prompt replies. The program still does not work as I hope. On compilation I get the following messages:- 16exer4b.c: In function ‘main’:
16exer4b.c:38: error: expected ‘)’ before ‘;’ token
16exer4b.c:45: warning: missing terminating ' character
16exer4b.c:45: error: missing terminating ' character
16exer4b.c:68: error: called object ‘10’ is not a function
16exer4b.c:68: error: expected ‘)’ before ‘}’ token
16exer4b.c:68: error: expected expression before ‘}’ token
16exer4b.c:68: error: expected declaration or statement at end of input

On running the program I get the query to Enter 1 to select file to display 0 to exit
When I reply the the above repeats indefinably.

The program code follows. Can you help? Thanks.

/* Programs prompts user to enter name of file to display on screen */
/* user is given to option to display another file until the user ends */
/* program */

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

#define BUFSIZE 100

main()
{
	char buff[60];
	//char filename[60];
	FILE *fp;
	int reply;
	int i;
		
	for( i = 0; i < sizeof( buff ) ;i++) 
	{
		buff[i] = '\0';
	}
	reply = 1;
	
	while( reply == 1 ) 
	{
		printf( "Enter 1 to select file to display 0 to exit " );
		scanf( "%d", &reply );
		getchar();

		if ( reply == 0 )
		 continue; //loop again (effectively breaking out if reply is 0
				
 		puts( "Enter name of text file to display: " );
		fgets( buff, sizeof(buff), stdin );

		if ( buff[strlen(buff)-1 ] == '\n'
			( buff[strlen(buff)-1 ] = '\0'; //remove newline from 
									 //string

		/* change any backslash char's to forward slash, in buffer */

		for ( i = 0; i < strlen[buff]; i++) 
		{
			if ( buff[i] == '\' )
				buff[i] = '/';
		}

		/* Open the file for reading. */

		if ( ( fp = fopen(filename, "r")) == NULL )
		{
			fprintf( stderr, "Error opening file.\n" );
			exit(1);
		}

		/* If end of file not reached, read a line and display it.*/

		while((fgets( buf, BUFSIZE, fp)) != NULL) 
		{
			printf( "%s", buf );
		}

		fclose(fp);
                return 0;
	} //end of while(reply == 1)
         //	return 0;
}

Can anyone help? New programmer trying to write Program that prompts user to enter name of file to display on screen.
User is given to option to display another file until the user ends program. it is not working and. After compilation when 1 is chosen it scroll the question enter name of the text file name to display. when 0 is chosen the program does the same.

Many thanks

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

#define BUFSIZE 100

main()
{
	char buf[BUFSIZE];
	char filename[60];
	FILE *fp;
	int reply;
		
	
	
		printf( "Enter 1 to select file to display 0 to exit " );
		scanf("%d", &reply);
	
		while( reply = 1 )
			puts( "Enter name of text file to display: " );
			gets(filename);
		
			
	
		/* Open the file for reading. */
		if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		/* If end of file not reached, read a line and display it.*/
	
		while( !feof(fp) )
			{
				fgets( buf, BUFSIZE, fp);
				printf( "%s", buf );
			}
	
		fclose(fp);
		
		
		return (0);
}

Remove line 37 and 38, and copy these two lines into it's place:

37. if (( buff[strlen(buff)-1 ]) == '\n')

38.   buff[strlen(buff)-1 ] = '\0'; //remove newline from buff

It's never correct to have main(), or void main(). There are only two forms of main declaration, that are correct:

int main(void) {

   return 0;
}

//or

int main(int argc, char *argv[]) {


  return 0;
}

The argc is the integer count of the number of arguments being passed into the program (typically, by a redirection of standard input), and char *argv[] is an array of pointers to char, which is storing the actual string inputs.

The name of your program itself, will be found in char *argv[0] = "My_Program's_Name".

Hello adak, edited code as your suggestions and did not work. When compiled got these messages:- 16exer4b.c: In function ‘main’:
16exer4b.c:43: error: array subscript is not an integer
16exer4b.c:45: warning: missing terminating ' character
16exer4b.c:45: error: missing terminating ' character
16exer4b.c:46: error: expected ‘)’ before ‘;’ token
16exer4b.c:47: error: expected expression before ‘}’ token

How do I redirect output to the printer. I don't have LPT1 port so stdprn() function does not work. I Have usb2 port.
Can you help?

Thanks.

Code follows:-

/* Programs prompts user to enter name of file to display on screen */
/* user is given to option the display another file until the user ends */
/* program */

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

#define BUFSIZE 100

int main()
{
	char buff[60];
	char filename[60];
	FILE *fp;
	int reply;
	int i;
		
	for( i = 0; i < sizeof( buff ) ;i++) 
	{
		buff[i] = '\0';
	}
	reply = 1;
	
	while( reply == 1 ) 
	{
		printf( "Enter 1 to select file to display 0 to exit " );
		scanf( "%d", &reply );
		getchar();

		if ( reply == 0 )
		 //break; //loop again (effectively breaking out if reply is 0
			break;
		
 		puts( "Enter name of text file to display: " );
		fgets( buff, sizeof(buff), stdin );

		if (( buff[strlen(buff)-1 ]) == '\n')
			buff[strlen(buff)-1 ] = '\0'; //remove newline from buff

		/* change any backslash char's to forward slash, in buffer */

		for ( i = 0; i < strlen[buff]; i++) 
		{
			if ( buff[i] == '\' )
				( buff[i] = '/' );
		}

		/* Open the file for reading. */

		if ( ( fp = fopen(filename, "r")) == NULL )
		{
			fprintf( stderr, "Error opening file.\n" );
			exit(1);
		}

		/* If end of file not reached, read a line and display it.*/

		while((fgets( buff, BUFSIZE, fp)) != NULL) 
		{
			printf( "%s", buff );
		}

		fclose(fp);
		
		//return 0;
	} //end of while(reply == 1)
	return 0;
}

Can anyone help? New programmer trying to write Program that prompts user to enter name of file to display on screen.
User is given to option to display another file until the user ends program. it is not working and. After compilation when 1 is chosen it scroll the question enter name of the text file name to display. when 0 is chosen the program does the same.

Many thanks

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

#define BUFSIZE 100

main()
{
	char buf[BUFSIZE];
	char filename[60];
	FILE *fp;
	int reply;
		
	
	
		printf( "Enter 1 to select file to display 0 to exit " );
		scanf("%d", &reply);
	
		while( reply = 1 )
			puts( "Enter name of text file to display: " );
			gets(filename);
		
			
	
		/* Open the file for reading. */
		if ( ( fp = fopen(filename, "r"))  == NULL )
			{
				fprintf( stderr, "Error opening file.\n" );
				exit(1);
			}
	
		/* If end of file not reached, read a line and display it.*/
	
		while( !feof(fp) )
			{
				fgets( buf, BUFSIZE, fp);
				printf( "%s", buf );
			}
	
		fclose(fp);
		
		
		return (0);
}

On line 43, replace the [ char with (, and the ] char with ).

On line 59, change

while((fgets( buff, BUFSIZE, fp)) != NULL)

to

while((fgets( buff, sizeof(buff), fp)) != NULL)

Since you're not using BUFSIZE anymore.

You may not have a parallel printer port, but C automatically opens stdprn as a file, when it starts your program. I don't have a code sample of it, and haven't used that function, since DOS days. (I'm sure Google has something newer than that!). Check it out, but it's easy (if it's what you want), to start your program from the console window "command line", with:

myprogramName > nameOfFile

and then just print the nameOfFile file, like you would any other file.

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.