Home:ALL Converter>Conflicting types for text to binary converter?

Conflicting types for text to binary converter?

Ask Time:2015-02-28T03:25:58         Author:Sam Wylock

Json Formatter

I am trying to create a program that takes a text file and converts it to a binary file. I have created the method to do so, but when I pass it the input and output files, I get a few errors:

unix1% gcc -Wall -Wextra main.c
main.c: In function 'main':
main.c:23:3: warning: implicit declaration of function 'txtbin'
main.c:23:8: warning: assignment makes pointer from integer without a cast
main.c: At top level:
main.c:30:7: error: conflicting types for 'txtbin'
main.c:23:10: note: previous implicit declaration of 'txtbin' was here
main.c: In function 'txtbin':
main.c:40:7: error: incompatible types when assigning to type 'struct FILE *' from type 'FILE'
main.c:41:7: error: incompatible types when assigning to type 'struct FILE *' from type 'FILE'
main.c:54:5: warning: implicit declaration of function 'strlen'
main.c:54:14: warning: incompatible implicit declaration of built-in function 'strlen'

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 255
#define MINLEN 0
#define NUMCHAR 1
#define NUMBYTE 4

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

  FILE* txtf;
  FILE* binf;

 if(argc != 4){
   fprintf(stderr, "Check usage");exit(1);
 }
 if((txtf =fopen(argv[2], "w+"))==NULL){
   fprintf(stderr, "Could not open text file: %s\n", argv[2]);exit(1);
 }
 if((binf =fopen(argv[3], "w+b"))==NULL){
   fprintf(stderr, "could not open binary file: %s\n", argv[3]);
 }

  binf = txtbin(txtf,binf);
  //bintxt();

  return 0;

}

FILE* txtbin(FILE in, FILE out){
  FILE *ifp;
  FILE *ofp;
  int tmpint = 0;
  unsigned char tmpchr = 0;
  char tmpstr[MAXLEN];

  ifp = in;
  ofp = out;

  while(fscanf(ifp, "%s \t %i \n", tmpstr, &tmpint) == 2){
    tmpchr = strlen(tmpstr);
    fwrite(&tmpchr, sizeof(tmpchr), NUMCHAR, ofp);
    fwrite(tmpstr, sizeof(tmpstr[0]), tmpchr, ofp);
    fwrite(&tmpint, sizeof(tmpint), NUMBYTE, ofp);
  }

  fclose(ifp);
  fclose(ofp);

  return ofp;
}

I know I have a few warnings, but I am most concerned with just getting the program to output the binary file for the respective text file.

By the way, here is the text file:

hello 32 goodbye 56 my 1 name 77 is 91 andrew 3

hello   32
goodbye 56
my      1
name    77
is      91
andrew  3

Author:Sam Wylock,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28772772/conflicting-types-for-text-to-binary-converter
yy