Thread: file input/output

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    file input/output

    i have a question about file input/output. before reading the file do i have to have a file call test.txt in c drive. because i have a file but it wont compile for some reason
    Code:
    #include<stdio.h>
     
    int main()
    {
      FILE *fp;
      fp=fopen("c:\\test.txt","r");
      fclose(fp);
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Whether the file exists or not will not affect compilation. The code you posted should compile, so I don't know what is causing your problem.

    Post your compile error. Note that you should always post your compile error when you make these types of threads.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Your code compiles well. I cant see any issue. What is it that you are trying to do?

  4. #4
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    It compiles fine, but causes exception if the file doesn't exist. Maybe you have a 'verbose' switch on a compiler or something. Remember, it's a *VERY* bad practice not to do error checking, even in smt as simple as this. Try adding check for NULL pointer on fp.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM