Thread: file opening

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    file opening

    i am writing a program that has the user input a file and then the file goes through two functions. I am having trouble opening the file. here is the code.

    Code:
     
         printf("Enter the file to open: ");
         scanf("%s", &f);
         if((fp = fopen(f,"rt")) == NULL)
         {
           printf("NO SUCH FILE");
         }
         fgets(f, sizeof(f), stdin);
         fopen("fp", "r");
         while((fp = fopen(f,"rt")) == NULL)
         {
           fgets(f, sizeof(f), stdin);
           DI = dividing(com, one, two);
           FI =  performance( com, one, two);
         }
         fclose(fp);

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fopen("fp", "r");
    Huh?

    [edit]A basic skeleton:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char filename[FILENAME_MAX];
       FILE *file;
       fputs("filename? ", stdout);
       fflush(stdout);
       if ( fgets(filename, sizeof filename, stdin) != NULL )
       {
          char *newline = strchr(filename, '\n');
          if ( newline != NULL )
          {
             *newline = '\0';
          }
          file = fopen(filename, "r");
          if ( file != NULL )
          {
             /* ... */
             fclose(file);
          }
          else
          {
             perror(filename);
          }
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 11-28-2005 at 11:01 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM