Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy string from one file to another

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
hey,
I have a simple question, and I've been looking at this code for too long. I want to open up three seperate files, one to read, the other two to update. I have structures in the files that need to be changed from file1 to both file2 and file3. Now, file3 doesn't have the same structure, but I need to get the date in file1 to go to both of these other files...how can I do this?

Thanks
M
 
I'm not quite sure how your data structures are built or
what the file layout is, but it should be trivial to grab
the time you want ,cast to char *, and fprintf().

I get the idea that what you want is more complicated
for some reason tho...
 
sorry for being so broad, I read my post after, and I came up with stuff I missed.

Here's the portion of code:

struct Header
{
char time_stamp[12];
char node_name[8];
int num_records;
int num_dotds;
char reserved[228];
};

static int getTimestamp(file1_path, file2_path, file3_path)
char *file1_path, *file2_path, *file3_path;
{
FILE *file1_fp, *file2_fp, *file3_fp;
struct Header file1_header, file2_header;

memset((char *)&file1_header, ' ', sizeof(file1_header));

if(!(file1_fp = fopen(file1_path, "rb")))
{
printf("Unable to open %s file.", file1_path);
return(1);
}

if(fread((char *)&file1_header, 1, 256, file1_fp) <= 0)
{
printf(&quot;Can not read file %s&quot;, dcutoken_path);
fclose(dcutoken_fp);
return(1);
}

................same code for files 2 and 3

strncpy(file1_header.time_stamp,file2_header.time_stamp, 12);

fclose(dcutoken_fp);
fclose(splitdata_fp);
fclose(splitstrtsend_fp);

so would I go about it with fprintf instead?

Would it look like this:
fprintf(file2_fp, &quot;%s&quot;, file1_header.time_stamp);


BTW file3's contains only the date so the whole file looks like this:
mmddyyhhmmss

Thanks
 
ok, I think I got it, but I'm pretty sure there's a cleaner way to do it. Let me know what you guys think:

same code as before, then:

//open to read only//
if(!(file2_fp = fopen(file2_path, &quot;rb&quot;)))
{
printf(&quot;Unable to update %s file.&quot;, file2_path);
return(1);
}

strncpy(file2_header.time_stamp,file1_header.time_stamp, 12);

fprintf(file3_fp,&quot;%12.12s&quot;, file1_header.time_stamp);

//do I need to close before I reopen, or can I do a freopen
without having to close?//
fclose(file2_fp);

//now open it up to write//
if(!(file2_fp = fopen(file2_path, &quot;wb&quot;)))
{
printf(&quot;Unable to update %s file.&quot;, file2_path);
return(1);
}

if(!fwrite((char *)&file2_header, 1, sizeof(file2_header), file2_fp) )
{
printf(&quot;Can't write to file %s.&quot;, file2_path);
fclose(file1_fp);
fclose(file2_fp);
fclose(file3_fp);
return(1);
}

fclose(file1_fp);
fclose(file2_fp);
fclose(file3_fp);

...
}

Let me know if there's a better way to do it, or if this way, although I know not clean, is good enough.

Thanks
M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top