Home > Mobile >  fprintf places in odd value when processing char value
fprintf places in odd value when processing char value

Time:08-28

I am writing what should be a simple program, but I'm having an odd issue with fprintf I have not been able to solve.

I am reading a small CSV text file and writing those values to a separate file containing only the numeric values.

My CSV file looks like this.

000,001,002,
003,004,005,
006,007,008,
009,010,011,
255,255,255

There is a comma between each value and a return at the end of each line.

The code I am using is

#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv) {
  FILE * inputFile;
  FILE * outFile;
  char * filename;
  int records = 0;
  char line[15];
  char * sp;
  char appendString[] = ".hex";
  printf("useage: dec2hex inputFile.txt\n");

  // Check if a filename has been specified in the command
  if (argc < 2) {
    printf("Missing Filename\n");
    return (1);
  } else {
    filename = argv[1];
    printf("Read in Filename : %s\n", filename);
  }

  // Open file in read-only mode
  inputFile = fopen(filename, "r");

  if (inputFile == NULL) {
    printf("Hey! Failed to open the file\n");
    return (1);
  }

  strcat(filename, appendString);
  printf("write out filename : %s\n", filename);
  outFile = fopen(filename, "w");

  while (fgets(line, 15, inputFile) != NULL) {
    sp = strtok(line, ",");
    char Y_pos = atoi(sp);

    sp = strtok(NULL, ",");

    char X_pos = atoi(sp);

    sp = strtok(NULL, ",");
    char OType = atoi(sp);

    //print to file
    fprintf(outFile, "%c", Y_pos);
    fprintf(outFile, "%c", X_pos);
    fprintf(outFile, "%c", OType);

    records  ;
  }
  fprintf(outFile, '\0');

  printf("records = %d\n", records);
  fclose(inputFile);

  fclose(outFile);

  return (0);
}

This will write to a file ..hex

The output I'm expecting should be 000102030405060708090a0bffffff

However what I'm seeing is an odd value inserted $D0.

odd hex value

I'm expecting $0A which does happen afterwards. I have tried some other values in my CSV (from 0 to 50) and this for now seems to be the only value that is random.

The reason I'm using %c in my fprintf is that I only need values from 0-255.

My first question, is why the odd value when processing 010?

How can this be corrected?

I'm using the TCC compiler 0.9.26, but have gotten similar results when using VS.

Thanks

CodePudding user response:

However what I'm seeing is an odd value inserted $D0 $0D.

File was opening in text mode and when writing a code 10 ('\n'), incurred a "\n" to "\r\n" translation on OP's machine.

Instead open the file in binary mode.

// outFile = fopen(filename, "w");
outFile = fopen(filename, "wb");

Note: Other code short-comings exist.

CodePudding user response:

you do not this strtok and atoi magic or binary mode opening magic

int main(void)
{
    char line[1000];
    while (fgets(line, 100, stdin) != NULL)
    {
        int X,Y,O;
        if(sscanf(line, "%d,%d,%d,", &Y, &X, &O) != 3) { /* error handling*/ }
        else
        {
            printf("x ", Y);
            printf("x ", X);
            printf("x ", O);
        }
    }
}

https://godbolt.org/z/vvKGzdcvd

  •  Tags:  
  • c
  • Related