0

I am trying to convert strings and integers to binary using fscanf and fwrite to write them to an output file.

My input file:

a       100
ab      99
abc     98
abcd    97
abcde   96

Each space separating the string and int on each line is a tab.

Here is my main.c file where the magic should be happening (but is not):

#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 30


int main(int argc, char * argv[]){

  FILE *ifp;
  FILE *ofp;

  if(argc != 4){
    fprintf(stderr, "Usage: flag %s input-file output-file\n", argv[0]); exit(1);
  }

  if((ifp = fopen(argv[2], "r")) == NULL){   /* error check to make sure the input file is open*/
    fprintf(stderr, "Could not open file: %s", argv[2]); exit(1);
  }

  puts("input file open\n");

  if((ofp = fopen(argv[3], "wb")) == NULL){ /* Opens output file to write in binary*/
   puts("couldnt open output file\n"); exit(1);
  }

  puts("output file open\n");

  unsigned char tempstr[MAX_LEN];
  unsigned int tempint;

  while(fscanf(ifp, "%u     %u\n",(unsigned int*)&tempstr, &tempint) == 2){

    fwrite((const void*)&tempstr, sizeof(tempstr)+1, 1, ofp);
    fwrite((const void*)&tempint, sizeof(unsigned int), 1, ofp);
    puts("ran loop");

  }

  fclose(ifp);


  return 0;
}

When I run my code my while loop does not seem to be running(ie.the "ran loop" is not being output). Not sure where to go from here?

Also my calls at the command line are as follows:

./a.out main.c t1.txt t1.bin

Any help would be appreciated! Thanks!

7
  • fwrite((const void*)tempint, sizeof(unsigned int), 1, ofp); --> &tempint for starters Commented Feb 27, 2015 at 3:42
  • Likely fwrite(tempstr, strlen(tempstr)+1, 1, ofp);, but I an not certain how you want to read this back. Would help more but GTG Commented Feb 27, 2015 at 3:44
  • 1
    also is tempstr only size 1 ?? (being sizeof(unsigned char) ). It needs to be long enough to hold the maximum size of the first field (plus null terminator) Commented Feb 27, 2015 at 3:45
  • Try compiling your code with -Wall -Wextra and remove all the warnings first. The warnings generated by your code will point you in the right direction. Commented Feb 27, 2015 at 3:53
  • I just changed the tempstr and tempint to &tempstr and &tempint. And I added a #define MAX_LEN 30 then did unsigned char tempstr[MAX_LEN)]; Commented Feb 27, 2015 at 3:53

1 Answer 1

0

I believe this should get rid of the seg fault.

  1. The type should be char * instead of unsigned char *
  2. The array should have enough space to hold the required characters plus the null terminator.

.

char tempstr[30];
unsigned int tempint;

while (fscanf(ifp, "%s \t %i\n",tempstr, &tempint) == 2 ) {
    fwrite((const void*)&tempstr, sizeof(tempstr)+1, 1, ofp);
    fwrite((const void*)&tempint, sizeof(unsigned int), 1, ofp);
    puts("ran loop");
}

Also I guess you don't need main.c, while executing ./a.out ... etc. You can change check for argc != 3 instead of 4 in your code.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay this actually got the loop running. So now all of the information in t1.txt is now in t1.bin?
Well not exactly. Try reading the documentation for fwrite(). Also, print out to the console and see what sizeof(tempstr) gives you. You'll need %ld instead of %d as a format specifier for printf()
Okay so the sizeof(tempstr) is giving me 30, like what you set the size to be. That makes sense, I was trying to get the information in each line of the input file to be turned into binary then to the output file, which i want fwrite for, correct?
Could you tell me what fwrite is writing to my output file then?
I think, it is also writing garbage values that are out of bounds each time in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.