Thread: file I/O

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    Question file I/O

    hi

    i have a question about c programing file I/O

    i did this text file "term" and saved it at desktop

    C:\Users\MYNAME\Desktop\term.txt


    i wrote this code

    Code:
    #include <stdio.h>
    
    int main ()
    {
    FILE *open;
    open = fopen("C:\\Users\\MYNAME\\Desktop\term.txt" , "r");
    getchar ();
    }
    when i compile the program and run nothing happens, can some one help me.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since your program does nothing by design, that's what you would expect. fopen doesn't open notepad or anything, it merely allows your program to access the contents of the file, should you so desire.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    2

    reply

    so how can i open the file, or i can't do that in C

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Code:
    #include <stdio.h>
    #define array_size 300
    #define FNAME "C:\\Users\\MYNAME\\Desktop\term.txt"
    int main ()
    {  
          FILE *fptr;
         char buffer[array_size];//to save in the lines as longest as it can be
         fptr = fopen(FNAME, "r");
         if(fptr==NULL)
           exit(1);
    while(fgets(buffer,sizeof buffer,fptr)!=NULL)
             fputs(buffer,stdout);
    return getchar();
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    note that all '\' chars should be escaped in the file name
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about working on the indentation, as well?
    No to mention closing the file after it's finished!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM