473,498 Members | 891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fwrite()

96 New Member
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
int i,j,k,ch;
char buf[1];
char s;
clrscr();

printf("Enter the choice 1: Reading 2: Writing :");
scanf("%d",&ch);

fp=fopen("D:\sample.txt","a+");
if(fp==NULL)
{
printf("Error : Not able to Open the file....\n");
}
else
{
do
{

switch(ch)
{
case 1:
{
printf("Reading a file....\n");
fseek(fp,0, SEEK_SET);
while( !feof(fp) )
{
fread(buf, 1, 1, fp);
printf("%c", buf[0]);
}
break;
}
case 2:
{

printf("Press ESC to terminate writing data in the file\n");
printf("Enter the data : ");
fseek(fp,2, SEEK_SET);
do
{
s=getch();
fwrite(&s, 1, 1, fp); /* write struct s to file */
printf("%c",s);
}
while( (s != 27) );
break;
}
default:
{

printf("Entered a wrong option...\n");
break;
}
}

printf("\nEnter the choice 1: Reading 2: Writing 3: Leave :");
scanf("%d",&ch);
if(ch==3)
{
break;
}
}
while( (ch==1) || (ch==2) );

}

printf("\nPress any key to leave the process...\n");
fclose(fp);
getch();
return 0;
}


Actually the code is working fine No problem But while entering some data in the file i pressed enter then the control is taken to starting position of the file ....It is rewritting the data from the beginning in the same line....

if i pressed enter key then the data entering should be proceed from the necxt link i.e how the microsft word works.....
Jan 25 '07 #1
1 2044
rajesh6695
96 New Member
I think if i use fscanf() instead of getch it will work i think.....







Thanks and Regards
Rajesh G
rajeshg@infotechsw.com
Jan 25 '07 #2

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

Similar topics

3
2431
by: Antoine Bloncourt | last post by:
Hello everybody Sorry to bother you but I have a problem writing datas into a file ... I want to make a backup of my MySQL database and put the result into a ..sql file. To do this, I use...
23
17809
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
17
6098
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.');...
15
3848
by: Suraj Kurapati | last post by:
Hello, I'm having a rather strange bug with this code: for certain values of 'buf', a segmentation fault occurs when 'free(buf)' is followed by an 'fwrite()'. In the program output, there is no...
4
2468
by: ibrahimover | last post by:
typedef struct{ char name; int no; }TAM; typedef struct{ char name; char ch; }HARF;
3
4497
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
2
6288
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
10
2659
by: Sheldon | last post by:
Hi, I am trying to learn C from scratch and, though I do know how to program in Python, many things in C are hard to understand - even after reading the examples. I guess because so many...
12
5022
by: hemant.gaur | last post by:
I have an application which writes huge number of bytes into the binary files which is just some marshalled data. int len = Data.size(); //arrary size for (int i = 0; i < len; ++i)...
25
15503
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
0
7124
marktang
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,...
0
6998
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...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
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...
0
7375
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
5460
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
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
287
bsmnconsultancy
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...

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.