I am supposed to use get the user input of a filename and then open that file and print to the screen (the first line-- that I will do later), and I can't figure out what I am doing wrong. Thank you.

#include <stdio.h>

main()
{
	char s[1000];
	printf("Enter the filename to open: ");
	FILE *fr;
	fgets(s, 81, fr);
    fr = fopen("s", "r");
    	if (!fr)
        return 1;
    	while (fgets(s,1000, fr)!=NULL)
        fprintf(fr,"%s\n",s);
    	fclose(fr);
	return 0;
}

The correct way of using fgets is fgets( char*, size_t, file_stream ).
Since you pulling the user response from the std input stream ie the keyboard, you have to put stdin in place of fr since the stream under consideration is the std input and not the file stream.

Also have made some changes in your code where it was not correct.
The changes i made are marked in red.

#include <stdio.h>

int main()
{
    char s[BUFSIZ] = {'\0'} ;
    printf("Enter the filename to open: ");
    FILE *fr = 0;
    fgets(s, BUFSIZ, stdin);
    fr = fopen(s, "w");

    if (!fr)
        return 1;

    while ( fgets(s,BUFSIZ, fr) != NULL)
        fprintf(fr,"%s\n", s);

    fclose(fr);
    return 0;
}

In case you need the function prototype or reference to any standard C function to see if you are making use of the function in a correct manner, you can visit here.

For some file handling tuts visit here:
http://www.cprogramming.com/tutorial/cfileio.html

Having read a line from the user, you normally have to remove the newline before trying to use it as a filename.

if ( fgets( buff, sizeof buff, stdin ) != NULL ) {
  char *p = strchr( buff, '\n' );
  if ( p ) *p = '\0';
  fp = fopen( buff, "r" );
}

thank you for your help, except I am not a very bright person, so maybe I did it wrong again, because when I run the program, it runs into an error and basically aborts...

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

int main()
{
    char s[81] = {'\0'} ;
    printf("Enter the filename to open: ");
    FILE *fr = 0;
    fgets(s, 81, stdin);
	if ( fgets( s, 81, stdin ) != NULL ) 
	{
	  char *p = strchr( s, '\n' );
	  if ( p ) *p = '\0';
	  fr = fopen( s, "r" );
	}

    while ( fgets(s,81, fr) != NULL)
        fprintf(fr,"%s\n", s);

    fclose(fr);
    return 0;
}

thank you for your help, except I am not a very bright person, so maybe I did it wrong again, because when I run the program, it runs into an error and basically aborts...

You know, I just talked to my mechanic about my car. I told him "my car just did something wrong (error) and stopped (abort)". I'll give you one guess what his response was...

Please learn to explain your problem completely. Most of us aren't mind readers.

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

int main()
{
    char s[81] = {'\0'} ;
    printf("Enter the filename to open: ");
    FILE *fr = 0;    // this line must be before the executable
                     // statements, like printf
    fgets(s, 81, stdin);                 // read a line from the keyboard
    if ( fgets( s, 81, stdin ) != NULL ) // read another line 
    {                                    // and check for an error
      char *p = strchr( s, '\n' );
      if ( p ) *p = '\0';
      fr = fopen( s, "r" );
    }

    // if there was an error, read a line from an unopened file
    while ( fgets(s,81, fr) != NULL)
        fprintf(fr,"%s\n", s);

    fclose(fr);
    return 0;
}

What kind of error? You want to print the file to the screen or back to 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.