You are here:

C/Followup Question on Odd File Format

Advertisement


Question
I did a few tests yesterday on the odd file format. I used fnsplit to try to obtain the indivitual component of the path and the function does not work correctly. The original filename is cabinet_1273-1-v1.bom.3 but I only managed to retrieve 'cabinet_' as it's filename. So now I intend to retrieve each component using my own algo. But the algo was not working yet. You may help on this. :) Codes till now: -

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

  char *item = NULL, *spec = NULL, *line = NULL, *tmp = NULL, *tokenPtr = NULL, token[3];
  char outFile[MAXPATH];
  char inFile[MAXPATH];
  char drive[MAXDRIVE];
  char dir[MAXDIR];
  char file[MAXFILE];
  char ext[MAXEXT];
  int quantity = -1, count = 0;
  FILE *inFilePtr = NULL, *outFilePtr = NULL;

  argv[1] = "c:\\append\\cabinet_1273-1-v1.bom.3";

  // Path manipulations
  tokenPtr = strtok(argv[1], "\\");

  while (tokenPtr != NULL){
     strcpy(token[count], tokenPtr);
     printf("%s\n", token[count]);
     tokenPtr = strtok(NULL, "\\");
     count++;
  }
  printf("%d\n", count);

  fnsplit(argv[1], drive, dir, file, ext);
  printf("%s\n", drive);
  printf("%s\n", dir);
  printf("%s\n", file);
  printf("%s\n", ext);

  fnmerge(inFile, drive, dir, file, ext);
  printf("%s\n", inFile);
  fnmerge(outFile, drive, dir, "tmp", ext);
  printf("%s\n", outFile);

  if (argc != 1){
     printf("Usage: Reformat the content by replacing all white spacings with tabs\n");
     printf("Fail to run due to lack of input: PATH\\FILENAME.\n");
  }
  else{
     if ((inFilePtr = fopen(inFile, "r")) == NULL)
        perror("fopen");
     else{
        if ((outFilePtr = fopen(outFile, "w")) == NULL)
           perror("fopen");

        printf("Input File: %s\n", &inFile);
        printf("Processing the input file now. Please wait...\n");

        fseek(inFilePtr, 0, SEEK_SET);   // Set inFilePtr to the beginning of inFile
        fseek(outFilePtr, 0, SEEK_SET);   // Set outFilePtr to the beginning of outFile

        tmp = (char *) malloc(100);
        line = (char *) malloc(100);
        item = (char *) malloc(100);
        spec = (char *) malloc(100);

        while (!feof(inFilePtr)){
           fgets(tmp, 100, inFilePtr);   // Read a line in the file

           if (strstr(tmp, "Assembly") == NULL){
              fscanf(inFilePtr, "%d %s %s", quantity, &item, &spec);
              sprintf(line, "%d\t%s\t%s\n", quantity, &item, &spec);
              if (fputs(line, outFilePtr) < 0){    // Write reformatted line to a tmp file
                 perror("fputs");
                 break;
              }
           }
           else{
              if (fputs(tmp, outFilePtr) < 0){    // Copy unformatted line to a tmp file
                 perror("fputs");
                 break;
              }
           }
        }

        fclose(outFilePtr);
        fclose(inFilePtr);

        if (remove(inFile) == 0){
           if (rename(outFile, inFile) != 0)
              perror("rename");
        }
        else
              perror("remove");

        printf("Processing is done! You may verify the file now! :)\n");
     }
  }
  return 0;
}

At the mean time, I'll still continue to debug it. Hopefully will get it working today! :)  

Answer
I am really surprised to know that, the filename itself is a problem. I think, MS Windows has some bug!
OR
It could be due to the compiler you are using!

I have never faced this kind of problem on Unix.
So, what is the compiler that you are using?
If you are not using gcc, try to use that.
I think, gcc will not create this type of problems.  

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.