473,396 Members | 2,033 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,396 software developers and data experts.

Couldn't solve 5 seconds data send from 1 file to another.Pls help need.

// This code write random data into a file and every 5 seconds this //program send 5 seconds data into another file.

#include <HEADER FILES>

FILE *f1,*f2;

int main(int argc, char *argv[])
{
int i,j,m=0,n=0;
char buf[100];

f1 = fopen("//root//Example//output1.data", "wb+");
if(!f1) printf("File cannot open");

f2 = fopen("//root//Example//output2.txt", "wb");
if(!f2) printf("File cannot open");

srand ( time(NULL) );

for(i=1;i<=30;i++)
{
j=rand()%100;
fprintf(f1,"%d %d\n",i,j);
printf("%d %d \n",i,j);
sleep(1);

if(i%5 == 0)
{
fseek(f1,i*2-10, SEEK_SET );
m=fread(buf,2,10,f1);
n=fwrite(buf,2,10,f2);
printf("\nBuffer= %d %d\n",m,n); //Confirm Read and write
}
}
fclose(f2);
fclose(f1);
}


File "output1.data" showing the output:
1 37
2 52
3 16
4 98
6 13 //Don't have Number 5 data.
7 26
8 11
9 92
10 72

For "output2.txt" showing the output:
1 37
2 52
3 16
4 98
3 16 //Again start from line 3.
4 98
6 13
7 26

Question: Here I didn't get actual answer.Can anybody help me?

My expected answer is

For "output1.data" showing the output:
1 37
2 52
3 16
4 98
5 35 [5 th Second, So data will send 1-5 lines to "output2.txt" file]
6 13
7 26
8 11
9 92
10 72 [10 th Second, So data will send 6-10 lines to "output2.txt" file]

For "output2.txt" showing the output:
1 37
2 52
3 16
4 98
5 35
6 13
7 26
8 11
9 92
10 72

NB: This is not my homework.I am trying to solve this problem. 2 weeks always failed.If u have any new idea... please help me.

Thanks
Mostafijur
Oct 12 '07 #1
1 1408
RRick
463 Expert 256MB
First of all, please put [ code=c]...[\code] tags around your code.

Your code has several issues.
Expand|Select|Wrap|Line Numbers
  1.   f1 = fopen("//root//Example//output1.data", "wb+");
Why are you opening the file as a binary file? The printf statements do not print out binary information. They convert the integer values to ascii.

What's killing you is you are reading and writing to the same file and using fseek to reposition the data. Fseek deals with number of bytes, not number of lines or number of integers. When your output goes from line 9 to line 10, you are adding an extra byte.
9 92
10 72
You also have to deal with the end of line character, too.

Your loop variable starts at 1 instead of 0. This probably accounts for the missing entries in output2.


What to do? The simpliest thing to do is open 3 files. Write output1, read output1, and write output2. Now you don't have to worry about the fseek. You'll have to be careful about flushing output1 between writing and reading it.
Nov 6 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
1
by: BVM | last post by:
Hi, All: I have this error. It seems execution time is too long. Actually the execution time is about 30 seconds(I tested in Query analyzer). How do I solve this problem? ...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
31
by: Stephanie | last post by:
I have a newbie question (couldn't find the answer with google) How to show countdown from 10 to 0 seconds. Stephanie
2
by: Matt | last post by:
When we submit the form data to another page, we usually do the following: <form action="display.aspx" method="post"> will submit the form data and open display.asp in the current browser ...
2
by: ejs | last post by:
Please read the entire reponse, hopefully this clears up your questions. The data used that needs to go into the batchupload page comes from a server. My problem is that currently I write a file...
7
by: Mat | last post by:
I am developping multi-user windows application. i use Access database. user edit, add and delete data from database. Request: when an item is deleted ,added or modified by an user, all others...
6
by: Champika Nirosh | last post by:
Hi, I have two machine where I needed to have a extended TCP/IP protocol to make the link between the two machines Mean,I need to write a application that compress every data the machine send...
2
by: mostafijur | last post by:
Hello 1. A program writing some data line by line in a file every 1 second. Data is writing like: 1 30 2 44 3 45 ...
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: 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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.