// 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,1 0,f1);
n=fwrite(buf,2, 10,f2);
printf("\nBuffe r= %d %d\n",m,n); //Confirm Read and write
}
}
fclose(f2);
fclose(f1);
}
File "output1.da ta" 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.tx t" 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.da ta" 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.tx t" file]
6 13
7 26
8 11
9 92
10 72 [10 th Second, So data will send 6-10 lines to "output2.tx t" file]
For "output2.tx t" 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
1 1429 RRick 463
Recognized Expert Contributor
First of all, please put [ code=c]...[\code] tags around your code.
Your code has several issues. -
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 understand that now and it
seems to be fixed but the data I'm pulling from the HTML fields is not
being appended correctly do my Access DB....The field in the DB now
reads " ' ". I understand why it does that (my function) but what I
need it to read is...
|
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?
System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not...
|
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
|
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
|
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
<form action="display.aspx" method="post" target="_blank"> will submit the
form data
and open display.asp in a new browser
| |
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 to the server and then
the user has to pick this file and send it up to the batchupload page. The
batchupload page is not one of our page, instead it is another company's
page. This is there interface to do batch processing. We have no control...
|
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 user in
the network which has application running should automatically see the
change. in case of remove action, the item should disappear from the
listview.
|
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
out.. and also need to un compress at the receiving end..
futher explaing...
|
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
4 89 .........
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |