473,748 Members | 4,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem using fseek

Hello everyone...

I´ve a problem with my code, when i put this lines:

recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CU R); fwrite(&p1,size of(p1),1,fp);
getch();

The file was saved with correct values and with some windows informations
too!?

like this " 8   O r i g i n a l F i l e n a m e C m d . E x e l &  P
r o d u c t N a m e"

what are the problem?!

Kisses

Feb 6 '07 #1
14 3705
On Feb 6, 2:50 pm, "Maria Mela" <h...@netvisao. ptwrote:
Hello everyone...

I´ve a problem with my code, when i put this lines:

recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CU R); fwrite(&p1,size of(p1),1,fp);
getch();

The file was saved with correct values and with some windows informations
too!?

like this " 8 O r i g i n a l F i l e n a m e C m d . E x e l & P
r o d u c t N a m e"

what are the problem?!
Show the actual code you are using and not a snippet of it.
Simplify the code until you have the minimal fully working code that
demonstrates the problem.

As a wild guess, you might be trying to read an OLE compound document
using fread, which is destined to humor.

Feb 6 '07 #2
user923005 wrote:
On Feb 6, 2:50 pm, "Maria Mela" <h...@netvisao. ptwrote:
>Hello everyone...

I´ve a problem with my code, when i put this lines:

recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CU R); fwrite(&p1,size of(p1),1,fp);
getch();

The file was saved with correct values and with some windows informations
too!?

like this " 8 O r i g i n a l F i l e n a m e C m d . E x e l & P
r o d u c t N a m e"

what are the problem?!

Show the actual code you are using and not a snippet of it.
Simplify the code until you have the minimal fully working code that
demonstrates the problem.

As a wild guess, you might be trying to read an OLE compound document
using fread, which is destined to humor.
or p1 points to a struct with pointers... fwrite() obviously won't
serialise string pointers in a struct.

maybe also byte padding? who knows.
Feb 7 '07 #3
here´s my struct

struct customer {
int dd,mm,yy;
char cus_ac_no[15],cus_name[25],cus_add[45],cus_ph_no[17];
double cus_bal;
float cus_intrst;
}p1;

typedef struct customer cust;

FILE *fp,*ft;
int recsize;
char choice,any,ch,e no[23];
double dep,
void transac()//Transaccoes de um Cartao
{
int k,p,t,r;
system("cls");
k=valid();
rewind(fp);
if(k!=2)
{
// _setcursortype( _NOCURSOR);
calbox(); highvideo();
gotoxy(12,12); cprintf(" N£mero invalido! ");
gotoxy(19,14); cprintf(" Tente Novamente");
getch();
}
else
{
while(fread(&p1 ,sizeof(p1),1,f p)==1)
{
if(strcmp(p1.cu s_ac_no,eno)==0 )
{
calbox();
fflush(stdin);
choice=getchar( );
switch(choice)
{
/*-----------------------------------------------------*/
case '1':
calbox();
gotoxy(5,5); cprintf("xxxxxx :");
gotoxy(8,7);cpr intf("Euros :");
scanf("%lf",&de p);
p1.cus_bal+=dep ;
acc_info(&p1);
recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CU R); fwrite(&p1,size of(p1),1,fp);
getch();
break;
"user923005 " <dc*****@connx. comescreveu na mensagem
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
On Feb 6, 2:50 pm, "Maria Mela" <h...@netvisao. ptwrote:
Hello everyone...

I´ve a problem with my code, when i put this lines:

recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CU R); fwrite(&p1,size of(p1),1,fp);
getch();

The file was saved with correct values and with some windows informations
too!?

like this " 8 O r i g i n a l F i l e n a m e C m d . E x e l &
P
r o d u c t N a m e"

what are the problem?!
Show the actual code you are using and not a snippet of it.
Simplify the code until you have the minimal fully working code that
demonstrates the problem.

As a wild guess, you might be trying to read an OLE compound document
using fread, which is destined to humor.
Feb 7 '07 #4
You missed this bit:
" minimal fully working code "
Which means that it should at least compile and run and demonstrate
the problem.

I suspect you might have a problem with uninitialized data, or struct
alignment (if two different machines read and write the information)
but there is no way to know with what we have been given so far.

Since you are using non-standard Borland functions, it will probably
take a bit longer to get the answers because we will have to stub
those out.

Feb 7 '07 #5
Maria Mela wrote:
>
.... snip ...
>
fflush(stdin);
choice=getchar( );
switch(choice)
{
/*-----------------------------------------------------*/
case '1':
calbox();
gotoxy(5,5); cprintf("xxxxxx :");
gotoxy(8,7);cpr intf("Euros :");
scanf("%lf",&de p);
Why should anyone bother to analyze something that is not
compilable (no main, no #includes) and is obviously in error
(fflush does not function on stdin, no such functions as gotoxy,
cprintf) etc.

Post complete, compilable code, with non-standard garbage
eliminated, after cutting it down to 100 lines or less. By the
time you do that you will probably find the fault yourself. If you
don't know what is standard and what isn't, say so and the
non-portable non-standardisms will be rapidly pointed out to you by
the howling mob.

If you want help, make it easy for someone to help you.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 7 '07 #6
On Feb 6, 5:35 pm, CBFalconer <cbfalco...@yah oo.comwrote:
fflush does not function on stdin
That language is a little strong. fflush(stdin) clears the input
buffer on some systems, but one should know that it's not portable.

-Beej

Feb 7 '07 #7
"Beej" <be**@beej.uswr ites:
On Feb 6, 5:35 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>fflush does not function on stdin

That language is a little strong. fflush(stdin) clears the input
buffer on some systems, but one should know that it's not portable.
And that's a little weak. Passing a pointer to an input stream (such
as stdin) to fflush() invokes undefined behavior. Some
implementations may choose to define the behavior.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 7 '07 #8
Beej said:
On Feb 6, 5:35 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>fflush does not function on stdin

That language is a little strong.
More precisely, the behaviour of fflush on input streams is undefined.
fflush(stdin) clears the input
buffer on some systems, but one should know that it's not portable.
And indeed there is no reason to risk it, since portable alternatives
exist, and are trivial to code.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 7 '07 #9
Richard Heathfield <rj*@see.sig.in validwrites:
Beej said:
>On Feb 6, 5:35 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>>fflush does not function on stdin

That language is a little strong.

More precisely, the behaviour of fflush on input streams is undefined.
>fflush(stdin ) clears the input
buffer on some systems, but one should know that it's not portable.

And indeed there is no reason to risk it, since portable alternatives
exist, and are trivial to code.
That's not quite true, depending on what "input buffer" is being
cleared. For example, if input is from a keyboard, it might be useful
to zero the typeahead buffer, in case the user incorrectly anticipated
what the prompt was going to be:

The program prompts "Enter user name: "

The user types "fred" followed by a newline.

The system is being a bit sluggish. The user assumes that the
next prompt is going to be "Enter password: " and types "qLutGg8A"
followed by a newline.

The program was recently modified so a password isn't required,
and the next prompt is actually "Enter broadcast message: ".

Fred's password is now sent out to everybody on the system.

If a call to fflush(stdin) were done before the "Enter broadcast
message: " prompt was issued, *and* if this call discarded the
password characters, then this problem might be avoided. I've used
similar facilities in the past, but not in C. I don't know whether
fflush(stdin) actually works this way on any of the systems that
support it.

This is different from reading and discarding all input up to the next
newline, which is often all you really need.

But the fact remains, fflush(stdin) invokes undefined behavior, and
any code that depends on the behavior of an implementation that
chooses to define it is non-portable, perhaps dangerously so.

(BTW, don't bother guessing what "qLutGg8A" is. It's randomly
generated; I've never used it as a password, and I never will.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 7 '07 #10

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

Similar topics

8
15359
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
10
12942
by: Orion | last post by:
Hey, I was wondering if it was possible to determine if you hit 'EOF' using fseek? I'm using fseek to traverse through the file from start to end and capturing the data into a linked list structure. However, my loop doesn't seem to work well - it totally fumbles out actually: while ((a = fseek(fp,0,SEEK_CUR)) == 0){ // code here }
2
3553
by: cedarson | last post by:
I am writing a program and have been instructeed to use the 'fseek', 'ftell', and 'stat' functions, however, after looking in the online manual for each of these, I am still unsure on how to use them. In my program, I am to write a code that opens a file, uses 'stat' to determine the file size, use 'fseek' to move the offset of the pointer, and finally use 'ftell' to obtain the file pointer index. Will someone please help? Again, thanks...
4
1896
by: Sudip | last post by:
I'm new in c programming. I am writing a program which reads the ID3 V1 tag from a mp3 file and edits it. But everytime i try to take inputs , the first character of my album contains 0. So, it doesn't write anything at the start of album field. So, winamp cannot recognize the album name. Can someone pls help me? :-( The code for tag input and edit is: void edittag(long loc,char name){ FILE *fp;
3
2951
by: Chen ShuSheng | last post by:
HI, I am now study a segment of codes: ------------------------ printf("%p\t",fp); /*add by me*/ fseek(fp, 0L, SEEK_END); /* go to end of file */ printf("%p\t",fp); /*add by me*/ last = ftell(fp); cout<<"last="<<last<<"\t"; /*add by me*/ -------------------------
0
2032
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual Studio 2005). The problem occurred when I trying to opening and reading file: ============= C++ ============= void LoadSourceFile(char * fileName, unsigned char * data, int length)
6
6154
by: ericunfuk | last post by:
A basic question: When the documentation says "fseek() clears EOF indecator, does it mean when you seek over EOF, EOF is no longer at the original position and hence removed?Say, after I seek over the original EOF, when I fread() from a previous position that I know is before the EOF then fread will not be able to tell if it has encountered the original EOF? Thank YOU!
18
2097
by: Scott | last post by:
Hi, a problem with this following code is really bugging me. tform = fopen(country, "r"); fseek(tform, 9L, SEEK_SET); fgets(player2, 38, tform); printf("Player Name (save): %s", player); printf("Player Name (form): %s", player2);
7
2850
by: souravmallik | last post by:
Hello, I'm facing a big logical problem while writing a parser in VC++ using C. I have to parse a file in a chunk of bytes in a round robin fashion. Means, when I select a file, the parser will read first 512kb(IBUFFSIZE) of data, then move to next file and parse the same way. This way I can parse a number of file spreaded over different directory uniformly. I'm keeping a meta data in a file where I'm keeping the track of file parse...
0
8984
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, 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...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
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, 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...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
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 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...

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.