Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fopen and getw not working as expected 1

Status
Not open for further replies.

toddyl

Technical User
Sep 26, 2005
102
0
0
US
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
 
For reading numbers use fscanf
Code:
[COLOR=#a020f0]#include [/color][COLOR=#ff00ff]<stdio.h>[/color]

[COLOR=#2e8b57][b]int[/b][/color] main () {
  [COLOR=#2e8b57][b]FILE[/b][/color] *uniq_id_file; 

  [COLOR=#2e8b57][b]int[/b][/color] number = [COLOR=#ff00ff]0[/color];
  [COLOR=#2e8b57][b]int[/b][/color] gcount = [COLOR=#ff00ff]0[/color];

  uniq_id_file = fopen([COLOR=#ff00ff]"uniqid"[/color], [COLOR=#ff00ff]"rb"[/color]);
  [COLOR=#804040][b]while[/b][/color](fscanf(uniq_id_file, [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff]"[/color], &number)!=[COLOR=#ff00ff]EOF[/color])
  {
   printf([COLOR=#ff00ff]"number is: [/color][COLOR=#6a5acd]%d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color],number);
   gcount = number;
   printf([COLOR=#ff00ff]"gcount is: [/color][COLOR=#6a5acd]%d[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color],gcount);
  }
  fclose (uniq_id_file);
  [COLOR=#804040][b]return[/b][/color] [COLOR=#ff00ff]0[/color];
}
Output:
Code:
number is: 9000
gcount is: 9000
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top