Help with STDIN

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 369 Views - Last Post: 15 February 2019 - 02:31 PM Rate Topic: -----

#1 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Help with STDIN

Posted 14 February 2019 - 08:11 PM

I need help with using standard input to read a file. The way I used standard input is not what was asked of me. Here is my code:
 
#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <stdbool.h>

int main(){

int totalWords = 0;

int totalChar = 0;

int totalLines = 0;

bool word = false;

FILE *file;

char filename[100000], c;

printf("Enter filename to read\n");

scanf("%s", filename);

file = fopen (filename, "r");

if (file == NULL) {

printf("Invalid File\n");

} else {

c = fgetc(file);

while (c != EOF){

totalChar++;

if (!isspace(c) && !word) {

word = true;

totalWords++;

}if (isspace(c) && word) {

word = false;

}

if (c == '\n') {

totalLines++;

}

c=fgetc(file);

}

printf("total words = %d\n", totalWords);

printf("total characters = %d\n", totalChar);

printf("total lines = %d\n", totalLines);

}

fclose(file);

return(0);

}



I want to be able to run the program through linux like:
./WordCount.c < Sample.txt

instead of having the program ask for a file name

Is This A Good Question/Topic? 0
  • +

Replies To: Help with STDIN

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,066
  • Joined: 05-May 12

Re: Help with STDIN

Posted 14 February 2019 - 08:26 PM

stdin is file handle that is always open. So no need to ask for a filename.

You do realize that your scanf() call was reading from stdin, right?
Was This Post Helpful? 0
  • +
  • -

#3 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Re: Help with STDIN

Posted 14 February 2019 - 08:38 PM

Yes i know that scanf is reading from stdin thats why I thought my solution was correct. However my professor said that I did not handle standard input correctly and said I should be using something like fgetc(stdin) instead of the way I was doing it.

This post has been edited by Skydiver: 15 February 2019 - 11:51 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Re: Help with STDIN

Posted 14 February 2019 - 09:03 PM

I want to be able to use the redirect input symbol < when i run the program

This post has been edited by Skydiver: 15 February 2019 - 11:51 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#5 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2310
  • View blog
  • Posts: 4,418
  • Joined: 30-May 10

Re: Help with STDIN

Posted 14 February 2019 - 09:23 PM

> I want to be able to use the redirect input symbol < when i run the program
You don't have to do anything.

This reads from stdin
./myprog

This also reads from stdin (redirection from a file)
./myprog < file.txt

This also reads from stdin (as part of a pipeline)
cat file.txt | ./myprog
Was This Post Helpful? 0
  • +
  • -

#6 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Re: Help with STDIN

Posted 15 February 2019 - 11:29 AM

When I run the program and try to use the redirect symbol, it gives me "invalid file". But when I just use ./WordCount then type my filename, it works. I am not really sure why this is happening.

This post has been edited by Skydiver: 15 February 2019 - 11:50 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 4,365
  • Joined: 21-June 11

Re: Help with STDIN

Posted 15 February 2019 - 11:38 AM

When you run ./myprog < file.txt, it will read the filename from file.txt and then try to open the file with that name. So if file.txt does not contain a valid filename, you'll get an error.
Was This Post Helpful? 0
  • +
  • -

#8 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Re: Help with STDIN

Posted 15 February 2019 - 11:49 AM

I am a little confused... the file I am using specifically is called "Sample.txt" when I run the program like this:
./WordCount
Sample.txt


It works perfectly

but when I run
./WordCount < Sample.txt
I get an error. You said that I will get an error if my Sample.txt does not contain a valid filename but im not sure what that means since I can run it the other way.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,066
  • Joined: 05-May 12

Re: Help with STDIN

Posted 15 February 2019 - 11:50 AM

marcusrogers: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.
Was This Post Helpful? 0
  • +
  • -

#10 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Re: Help with STDIN

Posted 15 February 2019 - 11:53 AM

Ok sorry about that.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,066
  • Joined: 05-May 12

Re: Help with STDIN

Posted 15 February 2019 - 11:55 AM

When you do a redirection using the `<` at the command line, you can think of the contents of the file being typed in one character at a time into your program's calls read from the standard input.

I can't help but feel like you skipped a lecture from your teacher, or jumped over chapter/section from your book. What book are you using? It maybe easier for us to point you back to the correct chapter/section if the book is relatively common.
Was This Post Helpful? 0
  • +
  • -

#12 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 4,365
  • Joined: 21-June 11

Re: Help with STDIN

Posted 15 February 2019 - 12:00 PM

View Postmarcusrogers, on 15 February 2019 - 07:49 PM, said:

You said that I will get an error if my Sample.txt does not contain a valid filename but im not sure what that means


It means that the contents of Sample.txt should be the name of a file that exists and that you have read permission for.

Quote

since I can run it the other way.


When you run it the other way, the input you enter is "Sample.txt". That's a valid filename. When you run it as ./WordCount < Sample.txt, the input will be whatever is inside Sample.txt. So that must be a valid filename for the code to work.
Was This Post Helpful? 0
  • +
  • -

#13 marcusrogers   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 19

Re: Help with STDIN

Posted 15 February 2019 - 12:01 PM

i have not skipped class haha... He told us to read from standard input... he never told us to use the redirection symbol so most of the class got it wrong so he gave us the opportunity to redo the assignment. The book is called Computer Systems: A Programmer's Perspective (3rd Edition) if that helps.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,441
  • Joined: 25-December 09

Re: Help with STDIN

Posted 15 February 2019 - 12:02 PM

Quote

I am a little confused... the file I am using specifically is called "Sample.txt" when I run the program like this:

If you run the program like ./WordCount you should get your prompt to enter the file name and then you must provide the input file name.

If you run the program like ./WordCount < Sample.txt Sample.txt is not the name of your input file, it is the name of the file that will act as stdin.

Jim
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 4,365
  • Joined: 21-June 11

Re: Help with STDIN

Posted 15 February 2019 - 12:06 PM

View Postmarcusrogers, on 15 February 2019 - 08:01 PM, said:

i have not skipped class haha... He told us to read from standard input...


Which you're not doing. Or rather you're reading a filename from standard input, but that's the only thing you read from stdin. Everything else you're reading from the given file. It doesn't sound like that's what the instructor wanted (or told you to do). It sounds like the instructor wants you to read the actual input from stdin. No file names.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2