Minor help needed with file opening

This is a discussion on Minor help needed with file opening within the C Programming forums, part of the General Programming Boards category; I've finished with what needs to be done which open a text file format it a certain way such as ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Minor help needed with file opening

    I've finished with what needs to be done which open a text file format it a certain way such as removing white spaces at the beginning of lines etc. This is just something alittle extra I'm trying to do. Which is prompt the user to enter in the path/filename of a text file. Then the program will open it and do the rest, but when ever I type in the path/filename nothing happens, I'm pretty sure I screwed the syntax somewhere but I can't figure out where.




    Code:
    int
    main( void )
    {
          int exit_status = EXIT_SUCCESS;
          
          
          FILE *fp;     
          printf("\n\nType in the path and file name\n\n\n", fp);
          scanf(&fp );
          fp = fopen ; // Where I'm running into problems
          
          if( fp == NULL ){
              printf("\nPlease enter a valid file\n");
              exit_status = EXIT_FAILURE;
         
        
          int count = 0;
          int c, copy;
    
    	  while((c = fgetc(fp)) != EOF){
                   
              if(c == ' '&& copy == ' ') continue;
    		     else putchar(c);
    		     copy = c;
    		
              if( c == ' ' && c == '}' && copy == ' ' &&  copy == '}')
                 count -= 1 ;
      
              if( c == ' ' && c == '}' && copy == ' ' &&  copy == '}')
                 count += 1 ;
    		
    	}
    	
    	   
    	return 0;
          
          } 
         
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dylancougar
    Code:
          FILE *fp;     
          printf("\n\nType in the path and file name\n\n\n", fp);
          scanf(&fp );
          fp = fopen ; // Where I'm running into problems
    'fp' is a pointer to a 'FILE' object. It currently doesn't point to anything at all. You are using scanf incorrectly in a few ways.

    1 - Its first argument is always a string of characters, one or more of which denotes the subsequent argument's types.
    2 - You're trying to scan in a 'FILE' type, but what you really want is a string of characters.
    3 - Even if you had the first argument right, there is no way to read 'FILE'.
    4 - Even if there were, your pointer doesn't actually point anywhere, so it would be incorrect.
    5 - Even if it did point somewhere, you'd still be wrong, because you don't use the & operator on non-pointer variables with scanf.

    You are also using fopen incorrectly.
    1 - It takes 2 arguments, you're only giving it one...no...none.
    2 - It's a function call, so your use of it is wrong, because you're using it as you would were you assigning a function pointer its address.
    3 - Even if it were right, you'd be overwriting 'fp' with whatever you had scanned into it above. But you can't, so that portion is actually correct, but for the wrong reasons.

    What you really want is a buffer to read a string into, and then to pass that argument to [colo=blue]fopen[/color]. Read the FAQ on reading a line from the user. You want something like:
    Code:
    char filename[BUFSIZ] = {0};
    FILE *fp;
    
    fgets( filename, BUFSIZ, stdin );
    fp = fopen( filename, "r" );
    Or something to that effect. Naturally you'll have to trim off the newline and all that good stuff, but the FAQ section should get you started.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    If understand you and, the tutorial I just read basically I need a buffer
    to read and pass the string the fopen. What is what I did here, unless I screwed up somewhere. The buffer 'filename' is the where the user inputs text, that text is passed to fopen, which reads the text and opens the file name.

    Its compiles fine but when I run it, it will prompt for input, I put the input then, it pauses for a second then, I get this crash which I never seen before in programinng but its the crash you get when lets say, an applaciation you are running, stops working and you get this put up

    xxxxxxx.exe has encountered a problem and needs to close . We are sorry for the inconvenience."
    Could it be my complier, because it looks like it should've worked now? Heres the code, that part of the code.

    Code:
    char filename[BUFSIZ] = {100};
              FILE *fp;
              printf("\n\nType in the path and file name\n\n\n", filename);
              fgets( filename, BUFSIZ, stdin );
              fp = fopen( filename, "r" );

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    char filename[BUFSIZ];
              FILE *fp;
              printf("\n\nType in the path and file name\n\n\n");
              fgets( filename, BUFSIZ, stdin );
              fp = fopen( filename, "r" );
    ssharish

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You should also use fclose() on the file when you're done with it:
    Code:
    fclose(fp);
    After the filehandle is closed, you can't use it (just like before it was opened).

    BTW, you should probably use "rt" for the file mode, not "r". With the latter, the compiler uses the default, which might not be text mode ("t").
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21