?
Solved

Read Integers from text file

Posted on 2012-04-01
8
Medium Priority
?
490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 total points
ID: 37794748
while ((c = fgetc(fpNumbr)) != EOF)   // no ; here
  {
    if (c != '\n')
      printf("%c",c);
    else
      printf("*");
  }
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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
 
LVL 35

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 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

719 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