Solved

Read Integers from text file

Posted on 2012-04-01
8
453 Views
Last Modified: 2012-04-02
Hi -
I am lost and frustrated.   I cant seem to make the leap from reading the text file to displaying it in the format that I need.   At first I thought I could check data types and that didnt work, next I thought I could use the function fgetc() and that doesnt seem to be working.  Here's what I have so far...it currenly displays nothing.  I'm reading from a text file that has the following:
123456789
12345678
1234567
123456
12345
1234
123
12
1

/*~~~~~~~~~~~~~~~~~  oddNumb Function ~~~~~~~~~~~~~~~
Displays only odd numbers from text file: MyOutput.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void oddNumb(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */

      int fgetc(FILE* fpNumbr);
      int numb;
      int c;

/* STATEMENTS */

while ((c = fgetc(fpNumbr)) != EOF);
{
      if (c != '\n')
            printf(numbIn);
      else
            printf("*");
}
return;
}/*disyplays odd numbers from text file*/

I'm ready to pull my hair out and start walking backwards... Any help would be greatly appreciated.  Thanks!
0
Comment
Question by:mojeaux
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 37794571
INPUT:
123456789
12345678
1234567
123456
12345
1234
123
12
1

What do you want your output to be? Is it
123456789
*
1234567
*
12345
*
123
*
1

or something else?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
ID: 37794748
while ((c = fgetc(fpNumbr)) != EOF)   // no ; here
  {
    if (c != '\n')
      printf("%c",c);
    else
      printf("*");
  }
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 125 total points
ID: 37795506
fgetc only would read one digit at a time.

you better read line by line using fgets and convert the char buffer you got filled by fgets to integer for example by atoi or strtol.

you then check whether the number is an odd number and do appropriate printf.

Sara
0
 

Author Comment

by:mojeaux
ID: 37796030
Hi Sara ...
I'm looking for the output to be:
1*3*5*7*9
1*3*5*7*
1*3*5*7
1*3*5*
1*3*5
1*3*
1*3
1*
1
Where only the odd numbers show and the even numbers are replaced by the asterisk.

It's the conversion that stumps me.   It wants to read a string of numbers as one and I want to process each one separately to evaluate if it's odd or not.      Thanks so much for help.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 32

Expert Comment

by:sarabande
ID: 37796416
in my opinion the assignment text speaks of "numbers" what is different to "digits". so i would print like phoffric showed in his 2nd example only odd numbers and omit all even numbers.

for the output you posted i would read lines using fgets and then have a inner for loop where the counter n runs from 0 to strlen(buffer)-1. assign '*' to buffer[n] if it contains an 'even' digit.

to find out which characters are even digits you either could compare the buffer[n] with either '0', '2', '4', '6', '8', or you use a helper string with contents "02468" where you test by strchr if buffer[n] is element of. or you use the buffer[n] as 8-bit integer and do modulo 2 (using operator %) on it. if the result is 0 the digit is even and if it is 1 the digit is odd. note that buffer[n] also could be '\n' (linefeed) which is neither odd nor even (see also 1st comment of ozo).

Sara
0
 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 37796529
I noticed that you are not using numbIn. But no matter, c will do. Note that c is the ASCII char and not the binary number. However, the ASCII char and the binary number both happen to have the last bit the same. See
     http://www.asciitable.com/
I'm looking for the output to be:
1*3*5*7*9
...
In this case, you can have a single loop reading one char at a time (as you have done in the OP).
Since your text file consists of only digits and newlines, you need to test for three cases:
a newline, '\n'
an odd number
an even number
	while ( (c = fgetc(fpNumbr) ) != EOF)
	{
		  if (c == '\n' )
			  printf("\n");  /* newline */
		  else if( c%2 != 0)   /* is digit odd ? */
			  printf("%c",c);  /* yes, odd */
		  else   /* digit is even */
			  printf("*");
	}

Open in new window

Usually, reading one line at a time with a nested loop, as sara suggests, is the safest approach, but in your specific case, reading one char at a time with a single loop suffices.
0
 

Author Comment

by:mojeaux
ID: 37797502
Thanks so much for the clarification.   I can't wait to get home and try the new suggestions after work.  You are a life/sanity saver!
0
 

Author Closing Comment

by:mojeaux
ID: 37798832
Thanks so much for the assist.   I think I made this much harder than what it was due to  the errors that I was getting when trying to determine if EOF and it was trying to tell me I couldnt convert the int type.   Really threw me for a loop.     I wish I could assign everyone the 500, but I had to split the points.   Best regards, Mojeaux
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now