C/Writing to file using C
Expert: Prince M. Premnath - 11/5/2006
QuestionHello again
I changed the mode from write to append at the beginning of the program because (write mode will empty the .php file) and tried to rewind the file pointer to the beginning of the file at the last append process in order to append to the file from the beginning but it doesn't work, it appends to the end of the file, this is my program...
#include <stdio.h>
#include <time.h>
int main() {
FILE *file;
char line[800];
int i, c1, c2, c3, c4, c5, c6, c7, c8, BR, BT, bits;
float bps, kbps, kb, KBR, KBT, KBpsr, KBpst;
time_t seconds1, seconds2;
while (1)
{
FILE *fp1;
fp1 = fopen("/var/www/html/1/dataDL.php", "w");
fprintf (fp1, "<?\n");
fprintf (fp1, "$data=array(\n");
fclose(fp1);
FILE *fp2;
fp2 = fopen("/var/www/html/1/dataUL.php", "w");
fprintf (fp2, "<?\n");
fprintf (fp2, "$data=array(\n");
fclose(fp2);
FILE *fp3;
fp3 = fopen("/var/www/html/1/errorDL.php", "w");
fprintf (fp3, "<?\n");
fprintf (fp3, "$data=array(\n");
fclose(fp3);
FILE *fp4;
fp4 = fopen("/var/www/html/1/errorUL.php", "w");
fprintf (fp4, "<?\n");
fprintf (fp4, "$data=array(\n");
fclose(fp4);
for(i=0;i<=10;i++)
{
file = fopen("/proc/net/dev", "r");
//Try to Open the File
//if(file==NULL) {
//printf("Error: can't open file.\n");
//return 1;
//}
//else {
//printf("File opened successfully.\n");
fseek( file, 330, 0);
seconds1 = time (NULL);
//printf ("%ld seconds since January 1, 1970\n", seconds1);
fscanf(file, "%d ", &c1);
printf("\nno. of Bytes Recieved = %d\n", c1);
fseek( file, 389, 0);
fscanf(file, "%d ", &c2);
printf("no. of Bytes Transmited = %d\n", c2);
fseek( file, 341, 0);
fscanf(file, "%d ", &c3);
printf("no. of packets Recieved = %d\n", c3);
fseek( file, 398, 0);
fscanf(file, "%d ", &c4);
printf("no. of packets Transmited = %d\n", c4);
fseek( file, 346, 0);
fscanf(file, "%d ", &c5);
printf("no. of error in Recieved = %d\n", c5);
fseek( file, 405, 0);
fscanf(file, "%d ", &c6);
printf("no. of error in Transmited = %d\n", c6);
fclose(file);
time_t start_time, cur_time;
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 1);
/*Scan the file again*/
FILE *file2;
file2 = fopen("/proc/net/dev", "r");
//Try to Open the File
//if(file==NULL) {
//printf("Error: can't open file.\n");
//return 1;
//}
//else {
//printf("File opened successfully.\n");
fseek( file2, 330, 0);
seconds2 = time (NULL);
//printf ("%ld seconds since January 1, 1970\n", seconds2);
fscanf(file2, "%d ", &c7);
printf("\nno. of Bytes recieved2 = %d\n", c7);
fseek( file2, 389, 0);
fscanf(file2, "%d ", &c8);
printf("no. of Bytes Transmited2 = %d\n", c8);
fclose(file2);
// count the (kbps)
BR = (c7 - c1);
//printf("Bytes Recieved = %d\n", BR);
BT = (c8 - c2);
//printf("Bytes Recieved = %d\n", BT);
bits = (BR * 8);
//printf("bits Recieved = %d\n", bits);
kb = (bits / 1024);
//printf("kbits = %f\n", kb);
kbps = (kb / 1);
//printf("kbps = %f\n", kbps);
int errorDL = c5;
int errorUL = c6;
///////////////////////////////////////////////////
KBR = (BR / 1024);
KBpsr = (KBR / 1);
printf("Rate of kBps Recieved = %f\n", KBpsr);
KBT = (BT / 1024);
KBpst = (KBT / 1);
printf("Rate of kBps transmited = %f\n", KBpst);
FILE *fp5;
fp5 = fopen("/var/www/html/1/downrate.txt", "w");
fprintf (fp5, "%f", KBpsr);
fclose(fp5);
FILE *fp6;
fp6 = fopen("/var/www/html/1/uprate.txt", "w");
fprintf (fp6, "%f", KBpst);
fclose(fp6);
FILE *fp7;
fp7 = fopen("/var/www/html/1/dataDL.php", "a");
fprintf (fp7, "%3.3f,\n", KBpsr);
fclose(fp7);
FILE *fp8;
fp8 = fopen("/var/www/html/1/dataUL.php", "a");
fprintf (fp8, "%3.3f,\n", KBpst);
fclose(fp8);
FILE *fp9;
fp9 = fopen("/var/www/html/1/errorDL.php", "a");
fprintf (fp9, "%d,\n", errorDL);
fclose(fp9);
FILE *fp14;
fp14 = fopen("/var/www/html/1/errorUL.php", "a");
fprintf (fp14, "%d,\n", errorUL);
fclose(fp14);
}
FILE *fp10;
fp10 = fopen("/var/www/html/1/dataDL.php", "a");
fprintf (fp10, ")\n");
fprintf (fp10, "?>\n");
fclose(fp10);
FILE *fp11;
fp11 = fopen("/var/www/html/1/dataUL.php", "a");
fprintf (fp11, ")\n");
fprintf (fp11, "?>\n");
fclose(fp11);
FILE *fp12;
fp12 = fopen("/var/www/html/1/errorDL.php", "a");
fprintf (fp12, ")\n");
fprintf (fp12, "?>\n");
fclose(fp12);
FILE *fp13;
fp13 = fopen("/var/www/html/1/errorUL.php", "a");
fprintf (fp13, ")\n");
fprintf (fp13, "?>\n");
fseek(fp13, 0 ,0);
fclose(fp13);
time_t start_time, cur_time;
time(&start_time);
do
{
time(&cur_time);
}
while((cur_time - start_time) < 5);
}
}
thank you for your help.
-------------------------------------------
The text above is a follow-up to ...
-----Question-----
Hi Mr. Prince M. Premnath
I am working on Linux Fedora core 3 with gcc compiler, I want to write to a file in the following order
//Beginning of the file
<?
$data=array(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
)
?>
//End of the file
This values of the array will be updated every 5 seconds... so I opened the file at the first in write mode, then I made a loop for updating the values of the array in appende mode and then go to the beginning of the program and so on... my problem is when I get to the beginning of the file its opened in the write mode and all the rest of the file lines is deleted and over written (I want the following: when I write at the first line of the file, I want to keep the rest of the lines as they are until they are updated by the loop of the program and so on) I am using fprintf to write to the file
//first open the file in write mode
FILE *fp1;
fp1 = fopen("/var/www/html/data1.php", "w");
fprintf (fp1, "<?\n");
fprintf (fp1, "$data=array(\n");
fclose(fp1);
//the loop of updating the array
//
//some code here
//
//FILE *fp1;
//fp1 = fopen("/var/www/html/data1.php", "a");
//fprintf (fp1, "%f,\n", KBpsr);
//fclose(fp1);
//
//end of the loop
FILE *fp2;
fp2 = fopen("/var/www/html/data1.php", "a");
fprintf (fp2, ")\n");
fprintf (fp2, "?>\n");
fclose(fp2);
Thank you in advance.
-----Answer-----
Dear Mazin!
The problem is very simple , the first thing you have done is correct , and the second - opening the file in "a" is also correct but the problem is ,
when you open file in append mode after opening the file successfully it will return a pointer to the first character in the file !
- so if you perform any input / output operation in a file it will affect the first character in the file ,(thats the fault you have done)
Solution:
Move the file pointer manually before performing I/O operation in a file even though its opened in append mode
syntax: fseek(fp , 0 , SEEK_END); here fp is a file pointer and , 2nd arguement is the number of bytes to be moved from the 3rd arguement , 3rd arguement is position in file .
Regards !
Prince M. Premnath.
AnswerDear Mazin !
Iam not clear with your question !
But any how i'll give you some properties on append mode.
1. You can append file only at the end .
2. If you wish to append the file at the beginning its a bit difficult , without any precautions if you do so then your file will be overwrited , how to avoid this ?
1. Move the file pointer to the beginning .
2.copy all the data in a temp file.
3.write the data what you have to actually write on that file .( now the file will contain only the data you have written recently )
4. Now append the data what you just copied in a temp file .
Now your datas have been appended to the beginning of the file .
Note :
This is the standard procedure that almost all the editors supposed to do !
Regards !
Prince M. Premnath.