C Get a string from a file

1k views Asked by At

I want to get 2 variable strings of fixed length (10chars and 32chars) from a file and save them as variables to pass off later in my program and write them to a new file. I can write the data to a new file from user input but I can't seem to figure how to locate the data and store it for use so a user won't have to manually input 42 chars and risk error. The strings will vary in content and may vary in position in the file but will always come after a constant string "Serial Number =" for example. What if there is a known offset location where the stings start, can this make it easier? I was thinking fget or fread... But I can't get a working example.

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

int main(void)

{
FILE *f;
FILE * pFile;
char sn[11];
char uuid[33];

  if (f = fopen("test.txt", "rt"))
  {
    fseek (f,443,SEEK_SET);  //file offset location to begin read
    fread(uuid, 1, 32, f);   //Read the data of 32 chars
    uuid[32] = 0;
    fseek (f,501,SEEK_SET);  //file offset location to begin read
    fread(sn, 1, 10, f);     //Read the data of 10 chars
    sn[10] = 0;
    fclose(f);  //Close our file
    printf("The 32 character UUID:\n%s\n", uuid);  //Show read/extracted Data
    printf("The 10 character SN:\n%s\n", sn);      //Show read/extracted Data

    pFile = fopen ("testfile.exe","r+b");   //Open binary file to inject data

    fseek (pFile,24523,SEEK_SET);    //1st file offset location to begin write
    fputs (sn,pFile);                //Write our data!

    fseek (pFile,24582,SEEK_SET);    //2nd file offset location to begin write
    fputs (uuid,pFile);              //Write our data!

    fseek (pFile,24889,SEEK_SET);    //3rd file offset location to begin write
    fputs (uuid,pFile);              //Write our data!

    fclose(pFile);                  //Close our file

    printf ("Finished\n");
    }
return(0);
}

I worked and read up on it all weekend, I now get the desired results, reading data from one file and injecting into another. Although this works, It may not be the best method. I apologize in advance for the mis tagged post, I submitted from my mobile and didnt have access to my source. Thanks for all the input. I welcome more. I tried to document as I understood what I was doing.

2

There are 2 answers

1
game on On

you can use fgetc and count each char in one string and say if c==" " means another word and reset your count.

0
Armali On

Though your solution works, you may find it practical not having to predetermine the offsets in "test.txt" where the strings start, but rather letting the program search for them, e. g. using

#include <stdio.h>
#include <stdlib.h>

void find(FILE *f, const char *s)
{   // searches stream f for string s, positions f after s
    int c;
    long offset;
    const char *cp;
    for (; ; )
    for (offset = ftell(f), cp = s; ; ) // save starting point
        if (!*cp) return;   // at end of search string - found
        else
        if ((c = fgetc(f)) == *cp) ++cp;    // character match
        else
        if (c == EOF) printf("\"%s\" not found\n", s), exit(EXIT_FAILURE);
        else
        if (cp > s) { fseek(f, offset, SEEK_SET), fgetc(f); break; }
}

and replacing

    fseek (f,443,SEEK_SET);  //file offset location to begin read

and

    fseek (f,501,SEEK_SET);  //file offset location to begin read

with

    find(f, "UUID =");          // really no space after "="?

and

    find(f, "Serial Number ="); // really no space after "="?

respectively.