How to Feed Input Files to C Program

  1. #1

    yukapuka is offline

    Registered User


    How to input test.txt into c program?

    if i have a program named prog.c and i have already converted it into an executable using gcc

    how do i test the prog.o in linux? what is the command for executing a test file where i have my inputs? so that at ./prog.o what extar command needs to be added with a test.txt fiel for inputs?

    so if i have to input some numbers and it is supposed to test those inputs and exit on fail?


  2. #2

    laserlight is offline

    C++ Witch laserlight's Avatar


    That depends on how you are reading input in that program. For example, if you are reading from standard input, then you could redirect input from the file to the program (or directly enter the test input yourself). If you wrote the program to take a file name as a command line argument and then open the file with that name, then you would run the program with the file name as a command line argument. Or, maybe your program prompts for a file name, upon which you would enter the file name as input when running the program, and then your program opens the file with that name.

    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)

    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

    Look up a C++ Reference and learn How To Ask Questions The Smart Way


  3. #3

    yukapuka is offline

    Registered User


    yes but what i am trying to do is feed the porgram a txt file with numbers to test out ,using linux commands


  4. #4

    Salem is offline

    and the hat of int overfl Salem's Avatar


    If your program reads standard input, then

    ./a.out < input.txt
    will work.

    Similarly, you can save all the output as well with
    ./a.out < input.txt > output.txt

    If your program accepts arguments, and tries to open a file for say argv[1], then use
    ./a.out input.txt


  5. #5

    yukapuka is offline

    Registered User


    Quote Originally Posted by Salem View Post

    If your program reads standard input, then

    ./a.out < input.txt
    will work.

    Similarly, you can save all the output as well with
    ./a.out < input.txt > output.txt

    If your program accepts arguments, and tries to open a file for say argv[1], then use
    ./a.out input.txt

    thank you this is exactly what i was looking for

    now i would like to extend the question just to understand this clearly

    if this is my program(hypothetically speaking)

    Code:

    #include <stdio.h> #include <stdlib.h>   int main(int argc, char * argv[]){  int year;   scanf("%d",year);        if(year>1528)         do_something       else         return 0;        return EXIT_SUCCESS;  }
    and i want my program to keep scanning the test.txt file until it reaches either END OF FILE or it breaks because it couldn't confirm the year was after 1528(which for me means a fail)

    i know i need to include the END OF FILE and also a while loop, but where exactly?


  6. #6

    Salem is offline

    and the hat of int overfl Salem's Avatar


    Ignore the fact that you're reading from a file, how would you detect EOF from the terminal?

    Hint: read the manual page for scanf and find out what it returns.


  7. #7

    yukapuka is offline

    Registered User


    Quote Originally Posted by Salem View Post

    Ignore the fact that you're reading from a file, how would you detect EOF from the terminal?

    Hint: read the manual page for scanf and find out what it returns.

    it says in the reference card that when it reaches EOF that it returns this in the int return of scanf

    so should i be doing "while(scanf(....);!=EOF);"


  8. #8

    new2C- is offline

    Registered User



  9. #9

    stahta01 is offline

    Registered User


    Quote Originally Posted by new2C- View Post

    I believe so, yes.

    I believe so, no!

    Tim S.

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson


langstonthaide.blogspot.com

Source: https://cboard.cprogramming.com/c-programming/162086-how-input-test-txt-into-c-program.html

0 Response to "How to Feed Input Files to C Program"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel