How to Read Input From File in C Compile

  1. #ane

    yukapuka is offline

    Registered User


    How to input test.txt into c programme?

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

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

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


  2. #two

    laserlight is offline

    C++ Witch laserlight's Avatar


    That depends on how you are reading input in that plan. For case, if you lot are reading from standard input, then yous could redirect input from the file to the program (or directly enter the test input yourself). If you wrote the program to accept a file name as a control line argument and and then open the file with that proper noun, then you would run the program with the file name equally a command line statement. Or, maybe your program prompts for a file proper name, upon which you would enter the file name equally input when running the programme, so your program opens the file with that name.

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

    I become maybe 2 dozen requests for assistance with some sort of programming or design problem every 24-hour interval. Most have more sense than to send me hundreds of lines of lawmaking. If they practise, I enquire them to discover the smallest case that exhibits the problem and send me that. By and large, they so detect the error themselves. "Finding the smallest plan that demonstrates the error" is a powerful debugging tool.

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


  3. #3

    yukapuka is offline

    Registered User


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


  4. #4

    Salem is online now

    and the hat of int overfl Salem's Avatar


    If your plan reads standard input, and then

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

    Similarly, you tin save all the output likewise with
    ./a.out < input.txt > output.txt

    If your program accepts arguments, and tries to open a file for say argv[1], then employ
    ./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 every bit well with
    ./a.out < input.txt > output.txt

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

    thanks this is exactly what i was looking for

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

    if this is my program(hypothetically speaking)

    Code:

    #include <stdio.h> #include <stdlib.h>   int principal(int argc, char * argv[]){  int year;   scanf("%d",year);        if(year>1528)         do_something       else         return 0;        return EXIT_SUCCESS;  }
    and i want my programme to keep scanning the test.txt file until it reaches either Stop OF FILE or information technology 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 too a while loop, but where exactly?


  6. #6

    Salem is online now

    and the hat of int overfl Salem's Avatar


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

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


  7. #vii

    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 notice EOF from the concluding?

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

    it says in the reference menu that when it reaches EOF that it returns this in the int render of scanf

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


  8. #8

    new2C- is offline

    Registered User



  9. #9

    stahta01 is online now

    Registered User


    Quote Originally Posted past new2C- View Post

    I believe and then, yes.

    I believe so, no!

    Tim S.

    "...a computer is a stupid motorcar with the ability to do incredibly smart things, while reckoner programmers are smart people with the power to practise incredibly stupid things. They are,in short, a perfect match.." Pecker Bryson


thomasbections.blogspot.com

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

Related Posts

0 Response to "How to Read Input From File in C Compile"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel