Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

Read Integers from text file

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
mojeaux
Asked:
mojeaux
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
phoffricCommented:
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
 
ozoCommented:
while ((c = fgetc(fpNumbr)) != EOF)   // no ; here
  {
    if (c != '\n')
      printf("%c",c);
    else
      printf("*");
  }
0
 
sarabandeCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mojeauxAuthor Commented:
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
 
sarabandeCommented:
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
 
phoffricCommented:
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
 
mojeauxAuthor Commented:
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
 
mojeauxAuthor Commented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now