473,395 Members | 1,948 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,395 software developers and data experts.

Problem using fseek

Hello everyone...

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

recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CUR); fwrite(&p1,sizeof(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 3676
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_CUR); fwrite(&p1,sizeof(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_CUR); fwrite(&p1,sizeof(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,eno[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,fp)==1)
{
if(strcmp(p1.cus_ac_no,eno)==0)
{
calbox();
fflush(stdin);
choice=getchar();
switch(choice)
{
/*-----------------------------------------------------*/
case '1':
calbox();
gotoxy(5,5); cprintf("xxxxxx:");
gotoxy(8,7);cprintf("Euros :");
scanf("%lf",&dep);
p1.cus_bal+=dep;
acc_info(&p1);
recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CUR); fwrite(&p1,sizeof(p1),1,fp);
getch();
break;
"user923005" <dc*****@connx.comescreveu na mensagem
news:11**********************@v45g2000cwv.googlegr oups.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_CUR); fwrite(&p1,sizeof(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);cprintf("Euros :");
scanf("%lf",&dep);
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.securityfocus.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...@yahoo.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.uswrites:
On Feb 6, 5:35 pm, CBFalconer <cbfalco...@yahoo.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_Keith) 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...@yahoo.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.invalidwrites:
Beej said:
>On Feb 6, 5:35 pm, CBFalconer <cbfalco...@yahoo.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_Keith) 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
On Feb 6, 11:29 pm, Keith Thompson <k...@mib.orgwrote:
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.
Yes, the behavior is undefined except where it is defined. That is
very extremely true.

In any case, these are all more complete answers than merely saying
fflush does not function on stdin.

Short-circuit to the FAQ: http://c-faq.com/stdio/stdinflush2.html

-Beej

Feb 7 '07 #11
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
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.
(Off-topic, obviously)

A much more realistic example, which often happens in practice, is
that for some reason the username is rejected (perhaps because the
system is slow and it timed out), so the next prompt is "Enter user
name". The user's password is read as their username, the login
fails, and their password is stored in a world-readable log file of
failed logins.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 7 '07 #12
Maria Mela writes:
struct customer {
(...)
}p1;
(...)
while(fread(&p1,sizeof(p1),1,fp)==1)
{
(...)
recsize = sizeof(p1);
fseek(fp,-recsize,SEEK_CUR); fwrite(&p1,sizeof(p1),1,fp);
Maybe you've opened the file in text mode instead of binary mode.
That'll break on some hosts. If so, add a 'b' to the mode flags in
fopen(). Another possibility is that the file is not seekable. Check
the return value from fseek() to catch that. And from fwrite(), while
you are at it.

--
Hallvard
Feb 7 '07 #13
Beej wrote:
On Feb 6, 11:29 pm, Keith Thompson <k...@mib.orgwrote:
>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.

Yes, the behavior is undefined except where it is defined. That is
very extremely true.

In any case, these are all more complete answers than merely saying
fflush does not function on stdin.
More complete, but possibly less useful.

What does `fflush(stdin)` do? We don't know: it isn't defined /by C/.
Should we use it in our code? No: it isn't defined /by C/.

The knowledge that /some/ systems do /something defined/ with it
doesn't seem to me to be that helpful, except I suppose in a
"nobody expects the Spanish Fflushquisition" kind of way.

--
Chris "electric hedgehog" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Feb 8 '07 #14
Chris Dollin wrote, On 08/02/07 09:51:
Beej wrote:
>On Feb 6, 11:29 pm, Keith Thompson <k...@mib.orgwrote:
>>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.
Yes, the behavior is undefined except where it is defined. That is
very extremely true.

In any case, these are all more complete answers than merely saying
fflush does not function on stdin.

More complete, but possibly less useful.

What does `fflush(stdin)` do? We don't know: it isn't defined /by C/.
That is fine.
Should we use it in our code? No: it isn't defined /by C/.
This I disagree with. Better is:

Should we use it in our code? Only if it is documented for all your
platforms of interest *and* you need the functionality it provides,
*and* you comment it *and* you don't post the non-portable code to
comp.lang.c
The knowledge that /some/ systems do /something defined/ with it
doesn't seem to me to be that helpful, except I suppose in a
"nobody expects the Spanish Fflushquisition" kind of way.
The knowledge is useful. Sometimes you need to do things that standard C
provides no mechanism for, and in those situations you use the
appropriate implementation specific method, which *might* be doing a
flush of an input stream. I've certainly had good reason in the past to
clear keyboard buffers (I do mean that rather than discard to the next
newline), and if I have good reason to again I'll do it, and if the
documented method for the system(s) of interest is fflush(stdin) that is
what I will do, I just won't expect that part of the code to be portable.
--
Flash Gordon
Feb 8 '07 #15

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

Similar topics

8
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...
10
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...
2
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...
4
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...
3
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); ...
0
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...
6
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...
18
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);...
7
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...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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,...

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.