Thread: How to input test.txt into c program?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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
    Registered User
    Join Date
    May 2008
    Posts
    48
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    48
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    48
    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
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    I believe so, yes.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to test a user input vs a saved password
    By epsilon tau in forum C Programming
    Replies: 1
    Last Post: 03-13-2012, 01:16 PM
  2. Test-program
    By ScariuM in forum C Programming
    Replies: 6
    Last Post: 03-31-2011, 01:46 PM
  3. Enormous Input Test
    By samus250 in forum C++ Programming
    Replies: 51
    Last Post: 01-03-2009, 12:12 PM
  4. Segmentation fault with input test file
    By bentles in forum C Programming
    Replies: 20
    Last Post: 04-28-2002, 08:58 PM
  5. how to test if user input is an int
    By sizzle_chest in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2001, 05:58 PM