473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

padding a file

Hello,

I want to write a header line and some logs to a file. I cannot
construct the header line until I have seen the last log line. I
decided to pad the beginning of the file and then write the logs (all
text). After I have written out all the log lines, I seek to the
beginning of the file and write the header line. I want to know if the
code below is correct (since I am using fwrite).

I am not worried about wastage of disk space because I delete the file
once I have processed it.

Thanks

/************* CODE
************************************************** */
FILE *fp = NULL;

/** FUNCTION: BeginLog **/

/* open temporary file in text mode for writing */
fp = fopen("logs.tmp", "wt");
if (NULL == fp)
{
return -1;
}

/* padding - reasonable assumption that header is < 4000 bytes */
char szPadding[4096];
memset(szPadding, 0, 4096);
if (1 != fwrite(szPadding, sizeof(szPadding), 1, fp))
{
return -1;
}
/*
* fwrite will not translate '\n',want to read it as text
* Also must append a newline to padding before
* writing first log line in order to use fgets
*/
fprintf(fp, "\n");

/********************/
/** FUNCTION: EndLog **/

if (NULL != fp)
{
fseek(fp, 0, SEEK_SET);
/* header line passed in or global */
fprintf(fp, "%s\n", headerLine);
fclose(fp);
fp = NULL;
}
return;

/**** FUNCTION: read ***/

FILE *fp = fopen("logs.tmp", "rt");
char line[4096];
while (NULL != fgets(line, 4096, fp))
{
printf("%s", line);
}
return 0;

/************************************************** *********************/

Nov 15 '05 #1
3 5566
In article <11*********************@f14g2000cwb.googlegroups. com>,
marvind <ma********@yahoo.com> wrote:
I want to write a header line and some logs to a file. I cannot
construct the header line until I have seen the last log line. I
decided to pad the beginning of the file and then write the logs (all
text). After I have written out all the log lines, I seek to the
beginning of the file and write the header line. I want to know if the
code below is correct (since I am using fwrite). /* open temporary file in text mode for writing */
fp = fopen("logs.tmp", "wt");
Noted: text mode rather than binary.
/* padding - reasonable assumption that header is < 4000 bytes */
char szPadding[4096];
memset(szPadding, 0, 4096);
if (1 != fwrite(szPadding, sizeof(szPadding), 1, fp))
{
return -1;
}
:/*
:* fwrite will not translate '\n',want to read it as text
:* Also must append a newline to padding before
:* writing first log line in order to use fgets*/
That comment indicates to us that you are concerned about portability.
However:

a) In C89, conforming implementations need not support more than 254
text characters before the newline;
b) In C89, what is input in text mode need not compare equal to
what was output unless the output was restricted to printable characters,
horizontal space, and newlines -- so in text mode writing 0's
is not certain to write any particular size of padding
c) Your comment is incorrect: fwrite() in text mode *will* translate \n .
fprintf(fp, "\n");

--
Ceci, ce n'est pas une idée.
Nov 15 '05 #2
Thank you.

Nov 15 '05 #3
Hello again,

I wanted to make sure that I was not committing any other mistake
related to this topic.

Based on your input, I decided to use binary mode to read from and
write to the log file because:
1. The log lines have unprintable ascii as field delimiter.
2. Due to the way our code is structured, I may have to pass the
position returned by ftell and close the file. The file is reopened in
binary mode and then the code fseeks to this position and starts
reading from this position (I cannot change this). To avoid a mismatch
here, I think I will use binary mode consistently to read the file.
3. I will continue to use fprintf to write to the file and not fwrite
of structures to avoid portability issues when dealing with int,
floats, etc.
4. As before, I will use a character array to store the padding and
write that out.

Please let me know if I am committing a mistake.

Thanks

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Knoxy | last post by:
Hello, I've noticed one or two people post on this before but nobody seems to have replied so raising the issue again... is this a known IE6 CSS bug? I have placed the following in my...
13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
5
by: Hallvard B Furuseth | last post by:
Does struct assignment copy padding bytes? Some compilers do, but I couldn't find anything in the standard which says they must. What I need is for any padding bytes to contan initialized values...
3
by: floppyzedolfin | last post by:
Hi there. I'm coding an encryption / decryption program. At this very moment, I think I should be pretty close from the end, but there's something blocking me on my way. There's a "Padding is...
6
by: John Messenger | last post by:
I notice that the C standard allows padding bits in both unsigned and signed integer types. Does anyone know of any real-world examples of compilers that use padding bits? -- John
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.