input files in C

This is a discussion on input files in C within the C Programming forums, part of the General Programming Boards category; This is a simple question, but I don't use C often. I have a program which takes data from an ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    input files in C

    This is a simple question, but I don't use C often. I have a program which takes data from an input file, but I need to use C input funtions. I can either 1) redirect input at the command line (a.out < data.txt) or 2) have the user specity an input file at runtime.

    I am currently using the first option, but my problem is that I use getch() to accept keyboard input later in the program. I do not know how to "unredirect" the input from the data file to the keyboard. I would actually like to use the 2nd option, but the only method I know to take input from a file is ifstream. I CANNOT use that. Can somebody help me out here?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I would actually like to use the 2nd option
    Lookup the C I/O functions:
    - fopen()
    - fgets()
    - fclose()
    and more.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    Thanks alot

    I'm really glad I found this board...I was getting discouraged.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I was getting discouraged.
    Dispair no more... we're here to help If there's something you don't know/understand, take a good look round here (faqs, tutorials, use the search forums button etc). If you can't find your answer, post a message and someone will help you.

    Happy coding.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    dont worry!!!

    hi folk,

    use the second option and here is the code for , only
    if u did
    not know
    about it.....


    int main(int argc,char **argv)
    {
    FILE *fp;

    fp = fopen("your input file name", "r+");

    {

    / /u'r input file content is stored in file pointer fp

    //continue with what u want to do

    }
    fclose(fp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  3. How to get input from multiple files?
    By jumpyg in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 08:20 PM
  4. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  5. string input from files
    By jriano in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2003, 01:08 PM

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