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.
you can use fgetc and count each char in one string and say if c==" " means another word and reset your count.