11 Replies - 270 Views - Last Post: 22 October 2011 - 12:09 PM Rate Topic: -----

#1 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Quick Question about redirection

Posted 22 October 2011 - 10:19 AM

Hello all,

Just a quick question iam getting my input from a text file by doing ./a.out <intial.txt

the input for example 1 2 3 a or 1 2 a 3

I want to be able to read all the numbers in and when I get to a store that in my array as zero. Here is the code I have so far



int array[100];
int number;
int count=0;
while (scanf("%d",&number)==1){
     array[count]=number;
     count++;
}




This is in C

Is This A Good Question/Topic? 0
  • +

Replies To: Quick Question about redirection

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: Quick Question about redirection

Posted 22 October 2011 - 10:31 AM

You will have a couple of problems with your code. First your loop will exit when it comes across the non-number value of 'a', because scanf will not return 1, it will return eof (usually -1). The other problem will be, you will need to properly clear the error before you can use the input stream again.

You would probably be better off using fgets() to retrieve the entire line into a C-string then use sscanf() to retrieve the individual elements.

Quote

I want to be able to read all the numbers in and when I get to a store that in my array as zero. Here is the code I have so far


What do you want to happen to the numbers that are after your 'a'?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: Quick Question about redirection

Posted 22 October 2011 - 10:38 AM

Still store them but store a as 0

Yeah I could use fgets and sscanf but it will be harder is there anyother way I cant set a flag also cause if it reaches a it will terminate
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: Quick Question about redirection

Posted 22 October 2011 - 10:40 AM

Why don't you use zero for zero?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: Quick Question about redirection

Posted 22 October 2011 - 10:41 AM

yeah Its a task I need to use a
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: Quick Question about redirection

Posted 22 October 2011 - 10:58 AM

Please explain what you are actually trying to accomplish in more detail.

Are the "numbers" all a single digit (less than 10)?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: Quick Question about redirection

Posted 22 October 2011 - 11:01 AM

Um the numbers are single digits and they are stored in array of ints and a is read in the input it should be stored in the array as zero.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: Quick Question about redirection

Posted 22 October 2011 - 11:05 AM

Since every "number" is a single digit then instead of using scanf to input numbers input characters, check each character if it is an 'a' store zero, otherwise convert the character to a number and store that, be sure you skip leading white space.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: Quick Question about redirection

Posted 22 October 2011 - 11:07 AM

Hmm thats fine but how will The program now its end of the text file
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: Quick Question about redirection

Posted 22 October 2011 - 11:10 AM

When scanf(), or fgets() detect the end of file.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 assert(C)  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 20-July 11

Re: Quick Question about redirection

Posted 22 October 2011 - 11:18 AM

Does it require me to open the file up using fopen cause I am redirecting the file
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is offline

  • member icon

Reputation: 3038
  • View blog
  • Posts: 9,270
  • Joined: 25-December 09

Re: Quick Question about redirection

Posted 22 October 2011 - 12:09 PM

With the command ./a.out <intial.txt you are redirecting the standard input file from the console to the supplied file. The standard input file is automatically opened by your program. You do not need to open any file to do this.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1