Solved

Calculation using input from text file

Posted on 2012-04-01
13
442 Views
Last Modified: 2012-04-01
Good evening -
I am having some difficulty with a field that is being read from a text input file.   I need to determine if the value is an odd number.   However, when I try to calculate using the field I get sharing violations.   I've even moved it to another integer field, but I get lvalue errors.   Here's the code snippet below.   Am I using a pointer incorrectly here?

while ((fscanf(fpNumbr, "%d", &numbIn)) == 1)
{      
      oddno = fscanf(fpNumbr, "%d", numbIn);
      if (oddno % 2 == 0)
      printf("*");
      else
      printf("%d", oddno);
}
0
Comment
Question by:mojeaux
13 Comments
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794355
The problem is that fscanf's return value is not the value you need to test against. Your scanned value will be in numbIn and not in oddno as written.

int oddno;
while ((fscanf(fpNumbr, "%d", &oddno)) == 1)
{      
      if (oddno % 2 == 0)
         printf("*");
      else
         printf("%d", oddno);
}
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794360
I just removed the extra parenthesis.

int oddno;
while (fscanf(fpNumbr, "%d", &oddno) == 1)
{      
      if (oddno % 2 == 0)
         printf("*");
      else
         printf("%d", oddno);
}
0
 
LVL 45

Expert Comment

by:Kdo
ID: 37794361
Hi Mo,

The while() loop continues to grab consecutive integers from the input stream.  In fact, it can grab only one because the character that terminates the number isn't skipped.  When the fscanf() function is called again, it starts with the character that ended the previous conversion, so it reads no values.

You need to skip all of the non-digit items (space, newline, etc.) before calling fscanf again.


Kent
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794370
I didn't include all the original code, but here is part that reads and determines whether the number is odd/even.

I ran the modified code and here is what the output looked like:

code:

                  int oddno;
                  while (fscanf(fpNumbr, "%d", &oddno) == 1)
                  {      
                          printf("%d", oddno); // I added to see what came back from fscanf
                          if (oddno % 2 == 0)
                               printf("*\n");
                          else
                               printf("%d\n", oddno);
                  }


output:
.
.
.
Press any key to continue . . .
123456789123456789
12345678*
12345671234567
123456*
1234512345
1234*
123123
12*
11
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794374
Hi Kent,

I thought fscanf worked as you stated, but it appears to be skipping the newlines?

Anthony
0
 

Author Comment

by:mojeaux
ID: 37794403
Hi Anthony -
Here's the full function... i've commented out some sections because I'm testing.  
void oddNumb(FILE *fpNumbr, int *numbIn)
{

/* LOCAL DECLARATIONS */

int oddno;

/* STATEMENTS */

while ((fscanf(fpNumbr, "%d", &numbIn)) == 1)
{      
      /*fscanf(fpNumbr, "%d", numbIn);
      if (oddno % 2 == 0)
      printf("*");
      else*/
      printf("%d", numbIn);
      printf("*");
}  

return;
}/*disyplays odd numbers from text file*/

Right now the ouptput displays the following:
123456789*12345678*1234567*123456*12345*1234*123*12*1

However, the display should be:
1*3*5*7*9 new line
1*3*5*7* new line
1*3*5*7new line
1*3*5* new line
1*3*5 new line
1*3* new line
1*3 new line
1* new line
1 new line

I'm reading from a text file that looks like
123456789
12345678
1234567
123456
12345
1234
123
12
1

Hi Kent - I read your comments and I'm not sure how to eliminate the new lines from being read by the fscanf statement.   Should I check for " "?  Is this what the program sees when it reads from the text file?

Thanks!!
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794422
Ok, i see.

Here is what i think you need to understand. Fscanf is is reading the numbers not one character at a time but rather one number such as 123456789, 12345678, etc. Then it converts from ASCII to a integer representation of the numeruc characters. Do youunderstand?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 37794430
>> I get sharing violations
>> oddno = fscanf(fpNumbr, "%d", numbIn);
>> Am I using a pointer incorrectly here?

No. You need &numbIn to make numbIn a pointer to an int.
0
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 500 total points
ID: 37794434
I think what you need to do is read a single character at a time, determine if it represent a number between 0-9, and then determine if it is odd or even. Be careful to skip over carriage returns and line feedcharacters.
0
 

Author Comment

by:mojeaux
ID: 37794438
Hi Anthony -  thanks for the clarification.   I understand how the fscanf is working now.  

Would it be easier if I wrote to the text file, placing a space between each number?  Would the fscan then recognize each interger?   Or would I then have to identify the spaces like Kent mentioned earlier?
0
 

Author Comment

by:mojeaux
ID: 37794442
I see your last post and it answered my question.  Thanks!
0
 

Author Closing Comment

by:mojeaux
ID: 37794444
Appreciate the help tonight.   Hopefully I can get this wrapped up soon.   =)
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 37794446
That sounds like an easy adjustment to try. I think adding a space will work.
0

Featured Post

IT, Stop Being Called Into Every Meeting

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

Suggested Solutions

Title # Comments Views Activity
debug as  junit test 4 64
copyEvens challenge 6 63
java continue statement 10 74
Using ANSI C how to Read a .csv file 10 52
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

747 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

17 Experts available now in Live!

Get 1:1 Help Now