×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

fopen and getw not working as expected

fopen and getw not working as expected

fopen and getw not working as expected

(OP)
Hi,

I am working on a Solaris server and have a piece of C code that is meant to read in a text file that contains an integer and it should then increment the integer by 1, use it and then write the new value to the text file.

My code has:

FILE *uniq_id_file; -- this line is declared near the top of the file after the #include section

int number = 0;
int gcount = 0;

uniq_id_file = fopen("/vobs/src/tool/uniqid", "rb");
while((number=getw(uniq_id_file))!=EOF)
{
 printf("number is: %d\n",number);
 gcount = number;
 printf("gcount is: %d\n",gcount);
}
fclose (uniq_id_file);

My file uniqid has the single entry 9000 in it. When I run my C program the printf's are displaying:

Main number is: 959459386
Main gcount is: 959459386

Why is this?
How can I get it to assign the actual value of uniqid to gcount?

Thanks,

toddyl

RE: fopen and getw not working as expected

For reading numbers use fscanf

CODE

#include <stdio.h>

int main () {
  FILE *uniq_id_file;

  int number = 0;
  int gcount = 0;

  uniq_id_file = fopen("uniqid", "rb");
  while(fscanf(uniq_id_file, "%d", &number)!=EOF)
  {
   printf("number is: %d\n",number);
   gcount = number;
   printf("gcount is: %d\n",gcount);
  }
  fclose (uniq_id_file);
  return 0;
}
Output:

CODE

number is: 9000
gcount is: 9000

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close