Solved

Read Integers from text file

Posted on 2012-04-01
8
458 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 25 97
zeroMAx challenge 20 99
How To Loop - Python 19 86
Programatically extract date from website 8 77
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

816 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

9 Experts available now in Live!

Get 1:1 Help Now