Solved

Read Integers from text file

Posted on 2012-04-01
8
466 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 33

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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 33

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
maxMirror challenge 10 161
bunnyEars challenge 6 139
word0 challenge 3 100
split worksheet into new worksheets based on changing value in Column A (by means of an Excel VBA) 4 27
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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