Ask your own question, for FREE!
Computer Science 40 Online
OpenStudy (anonymous):

How do you read in a file from stdin? I need to include the text file to be read when compiling so I can't hard code a specific file name in my code. This code grabs a line from a file: fgets(inputLine, MAX_LENGTH, stdin) the 3rd parameter is the file pointer. However just stdin does not work, I must include something else to get it to read a line. What am I missing? Example of line when compiling: clang -Wall readInFile < textFile.txt

OpenStudy (anonymous):

This confuses me because stdin is expressly designed to work just like any other FILE*. Like, it's part of the point of the c library that it works like that. Are you sure your command line isn't what is doing it?

OpenStudy (anonymous):

I was using a Makefile, but since I didn't want to add the file in just yet I just did it on one line. I actually compile three programs at once. Compiling line: clang -Wall something.h something.c main.c < textFile.txt When I run, the program will just get stuck in a loop when trying to read the textFile. I put a printf in the loop and nothing was being printed out like it should have.

OpenStudy (anonymous):

Wait, are you trying to put in the text file at compile-time? You can't do that. You have to compile it and then run it with textFile.txt fed to it.

OpenStudy (anonymous):

I totally forgot that was how you did it >_< thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!