10 Replies - 3066 Views - Last Post: 31 October 2010 - 06:39 AM Rate Topic: -----

#1 KiddKoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 30-October 10

C program that reads "standard input file"

Posted 30 October 2010 - 09:32 PM

I am to write a program that reads the standard input file and count the digits, and all other variables, placing the totals in two separate variables.

I do not know exactly what the standard input file is. I told the program to open "stdin".... this is where my problem is I think.

I think the rest of the code should work.

This code compiles but does not run. I get a "Segmentation error." Don't know what that is exactly.

#include <stdio.h>

int main(void)
{
        int digit_cnt = 0, other_cnt = 0, c;
        FILE *f;

        f = fopen("stdin", "r");
        while ((c = fgetc(f)) != EOF)
                if (c >= '0' && c <= '9')
                        ++digit_cnt;
                else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ||
                        c == ' ' || c == '\n')
                        ++other_cnt;
        printf("%10s%10s\n", "digits", "other");
        printf("%10d%10d\n", digit_cnt, other_cnt);

        fclose(f);
        return 0;
}

This post has been edited by KiddKoder: 30 October 2010 - 09:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C program that reads "standard input file"

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: C program that reads "standard input file"

Posted 30 October 2010 - 09:35 PM

Welcome to DIC!

Please edit your posting (see the "EDIT" button in lower right). (If Edit is not available make a new posting on this thread and skip ( a ) below.)
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: C program that reads "standard input file"

Posted 30 October 2010 - 09:40 PM

Do you think you might have misread the requirements or the teacher might have mistyped them.

Standard input usually means the keyboard.

An input file is a file.

Standrad input file are not really 3 words that go together happily.

Do you think the assignment is actually to read from the keyboard?
Was This Post Helpful? 0
  • +
  • -

#4 KiddKoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 30-October 10

Re: C program that reads "standard input file"

Posted 30 October 2010 - 09:49 PM

View Postjanotte, on 30 October 2010 - 08:40 PM, said:

Do you think you might have misread the requirements or the teacher might have mistyped them.

Standard input usually means the keyboard.

An input file is a file.

Standrad input file are not really 3 words that go together happily.

Do you think the assignment is actually to read from the keyboard?


That would make more sense to me, however the exact words in the book are "Write a program that reads characters from the standard input file until EOF is encountered..." I have searched everywhere and cant find an example of this.

This post has been edited by KiddKoder: 30 October 2010 - 09:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: C program that reads "standard input file"

Posted 30 October 2010 - 09:59 PM

Hmmmm. Interesting. They didn't, earlier in the book say something like, the standard input file for the examples in this book (or chapter or whatever) will be xyz.txt or whatever did they?

Anyway, I am no real help to you in interpreting what they might be trying to say.

Anyone else have a better idea?

EDIT
Arrgh too tired, obviously.
See jimblumberg's clear headed thinking below.
Time for sleep for me it seems!

This post has been edited by janotte: 30 October 2010 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: C program that reads "standard input file"

Posted 30 October 2010 - 10:01 PM

When you start a program the operating system opens 3 files by default, stdin, stdout, and stderr. Stdin and stdout are normally the keyboard and monitor, and they are buffered. Stderr is usually the monitor and is not buffered.

Please see the following link stdin and standard streams


Jim
Was This Post Helpful? 1
  • +
  • -

#7 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: C program that reads "standard input file"

Posted 30 October 2010 - 10:04 PM

Exactly as janotte suggested, EOF is end of file. So either the instructions are wrong, or they meant read until end of line (enter key) is given.

Is it possible that the assignment is to read a file name from standard input? Then read the file until EOF...?
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: C program that reads "standard input file"

Posted 30 October 2010 - 10:20 PM

View Postno2pencil, on 30 October 2010 - 10:04 PM, said:

Exactly as janotte suggested, EOF is end of file. So either the instructions are wrong, or they meant read until end of line (enter key) is given.

Is it possible that the assignment is to read a file name from standard input? Then read the file until EOF...?


Actually if you type Control Z (Windows) or Control D (Linux) you will produce eof()

I'm not sure what it is on a Mac.

TRY
#include <iostream>
using namespace std;
int main()
{
   int number;
   cin >> number;  // Type the control and the correct character here.
   if(cin.eof())
     cout << "You entered eof()"
   return 0;
}



Jim

This post has been edited by jimblumberg: 30 October 2010 - 10:28 PM

Was This Post Helpful? 2
  • +
  • -

#9 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: C program that reads "standard input file"

Posted 30 October 2010 - 10:30 PM

Since Mac OSX is based on FreeBSD I would assume it would be the same on Mac as it is for Linux.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: C program that reads "standard input file"

Posted 30 October 2010 - 10:33 PM

You should not open stdin as it is already open. But the following should work:

while ((c = fgetc(stdin)) != EOF)



Jim

This post has been edited by jimblumberg: 30 October 2010 - 10:37 PM

Was This Post Helpful? 0
  • +
  • -

#11 KiddKoder  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 41
  • Joined: 30-October 10

Re: C program that reads "standard input file"

Posted 31 October 2010 - 06:39 AM

Thanks everyone for the suggestions.

I ended up just assuming that EOF meant the end of the user's input into stdin, which I set as carriage return.

Here is my final code for anyone curious.

#include <stdio.h>

int main(void)
{
        int digit_cnt = 0, other_cnt = 0, c;

        while ((c = getc(stdin)) != '\n')
                if (c >= '0' && c <= '9')
                        ++digit_cnt;
                else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ||
                        c == ' ')
                        ++other_cnt;
        printf("%10s%10s\n", "digits", "other");
        printf("%10d%10d\n", digit_cnt, other_cnt);

        return 0;
}

This post has been edited by KiddKoder: 31 October 2010 - 06:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1