c - Hexadecimal conversion from a file -


what wrong code? trying read in file line line , convert numbers in form of 0x"hex numbers" integers. returning 1 line , ends, input this

0x9c40

0x3b9ac9ff

0x754893gf

0x754893gf

0x754893gf

0x754893gf

  #include <stdio.h> #include <stdlib.h>    #include <stdio.h>   #define maxchar 1000  int main() {      file *fp;     char str[maxchar];     char* filename = "c:\\test.txt";     int number;      fp = fopen(filename, "r");    if (fp == null){       printf("could not open file %s",filename);       return 1;      }    while (fgets(str, maxchar, fp) != null)        number = (int)strtol(str, null, 0);     printf("%d\n", number);    fclose(fp);     return 0;     } 

you forgot enclose while block in curly braces:

while (fgets(str, maxchar, fp) != null) {     number = (int)strtol(str, null, 0);     printf("%d\n", number); } 

your code equivalent

while (fgets(str, maxchar, fp) != null) {     number = (int)strtol(str, null, 0); } printf("%d\n", number); 

which explains why 1 line of output.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -