Thread: how do i read an external file

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    how do i read an external file

    I looked it up in the c book I have but it wasn't very clear. I also tried to search the site but there were too many specific examples.

    I was wondering how I read a text file and then do something with it in a program. For example, look for a specific word in a text file (with a list of words).

    I think this is a simple problem but I couldn't really find a simple answer so I had to post it here. I hope there's someone who can help.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    To read you can use fscanf, fread, fgets, fgetc, getc ...
    To write you can use fprintf, fwrite, fputs, fputc...

    Try searching the web for these functions to see how they work.

    >For example, look for a specific word in a text file (with a list of words).
    Try do this without using files, and then translate the input/output functions so that they can read from/print to a file.
    Loading.....
    ( Trying to be a good C Programmer )

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    32

    I'll try that

    Alrighty, I'll try that. Thank You.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    checkout cplusplus.com

    from their site
    Code:
    /* fopen example */
    #include <stdio.h>
    int main ()
    {
      FILE * pFile;
      pFile = fopen ("myfile.txt","wt");
      if (pFile!=NULL)
      {
        fputs ("fopen example",pFile);
        fclose (pFile);
      }
      return 0;
    }

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Use this:

    C_Ref_Card.pdf

    It has all the info on functions and basic descriptions. For examples on use do a search on this forum. I did a string program for someone not to long ago that should provide a decent template for using them.

    That card is like the ULTIMATE refrence, I think.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    32
    Thanks mart_man00 and Lynux-Penguin. The C ref card was awsome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. How to convert char read from file into string
    By cruxxe in forum C Programming
    Replies: 7
    Last Post: 05-22-2002, 02:09 PM