NO

Author Topic: Problem with file handling  (Read 4281 times)

Langer.197

  • Guest
Problem with file handling
« on: January 04, 2013, 04:16:10 PM »
Might i ask another question?

I want to make this work.

Code: [Select]
fwrite(&userpoint, sizeof(struct point), 3, file);
It seems to work when i keep it in the "main-file".
It creates a new file with a name and takes the input in binary mode, but we have to put all our functions in another file.
I guess this is a problem of me not knowing how to use pointers, but no matter what i try, i get errors like those:

point.c(17): error #2048: Undeclared identifier 'file'.
point.c(17): error #2140: Type error in argument 4 to 'fwrite'; expected 'FILE * restrict' but found 'int'.

I declared "FILE *file" in the main-file.

It might help to know that this is the function call in the main-file

Code: [Select]
addpoint()
and this how this function looks like in the "functions-file"

Code: [Select]
struct point addpoint(){}
"fwrite" is used in that "addpoint"-function.


EDIT:

This is my header-prototype for this addpoint-function:

Code: [Select]
struct point addpoint();
But is can not get it together.
I know that i need to hand over the adress of the pointer, wich would be "&file".

But then i have no idea what to change in the functions-file.
Whenever i try to change sth. it complains about the header-proto, when i adjust the proto, i still wont work.

Anybody knows where my error in reasoning is?
Would be great. :)
« Last Edit: January 05, 2013, 02:16:43 AM by Stefan Pendl »

CommonTater

  • Guest
Re: Problem with file handling
« Reply #1 on: January 04, 2013, 04:47:34 PM »
I'm going to have to see your source for this...

If you go into the main menu -> Project -> Zip files  you can make a zip of your project, attach this zip to your reply and I'll take a look at it...

(If you are using the forum's quick reply, click the preview button then the attachments button at the bottom of that page)

In general disk files have to follow rigid formats ... you have to know exactly where whatever you write out is before you can read it back.  So you may need to use multiple files to make your job work.  A prime example of this is an inventory program, where each inventory item is in a fixed size struct, written out in some logical order, so that you can seek to its start and load only what is required.  Most often these "binary mode" or "random access" files are strictly limited to only those structs; Free Form files simply do not exist, except in the imagination of programmers.
 
Also be aware that something declared in one source file does not carry to another... That is to say if you declare int A in main.c it is no longer valid in extra.c ... this is why we use headers (.h)... we declare the variable (function, struct, etc) in one file, then create a header where we redeclare it as extern and include that header into the other file... Otherwise the compiler (which works file by file) loses track.

A trivial example of this would be...
Code: [Select]
// main.c
 
#include "extra.h"
 
int a = 0;
 
int _cdecl main (void)
  {
    int b;
 
    b = flub();
    return 0;   
  }
-------------
// extra .c
 
#include "extra.h"
 
int flub(void)
  {
     return a * 6;
  }
--------------
// extra .h
 
extern int a;     
 
int flub(void);
... where extra.h is used to convey the variable a from main to extra and the function flub() from extra to main.
 
« Last Edit: January 04, 2013, 05:00:01 PM by CommonTater »

Langer.197

  • Guest
Re: Problem with file handling
« Reply #2 on: January 04, 2013, 05:01:12 PM »
Okay, thanks for the quick reply. And for the time.
I try my best to not ask stupid answers, but sometimes i am stuck for such a long time that even my books and internet research does not help.
Might also be that i am just not able to connect the facts i know.
But well, thanks again. I attached the file.
Hope your okay with the german phrases. ;)



Quick info what the programm should do.

1. take a point the user wants to save (x,y,z)
2. take more points until the user wants to
  2.1 quit the program
  2.2 print all the points that are in the file already
3.when user open the file, he should be able to add more points (dont delete remaining points)
« Last Edit: January 04, 2013, 05:05:55 PM by Langer.197 »

CommonTater

  • Guest
Re: Problem with file handling
« Reply #3 on: January 04, 2013, 05:20:13 PM »
Ok, had a quick peek at your source...

I think your first problem is that you are telling it to write 3 structs, but you've only defined a single struct....

Code: [Select]
fwrite(&userpoint, sizeof(struct point), 3, file);
Is going to try to write 3 times sizeof(struct point) ... and that's memory you don't own since you've only defined a single struct.
 
Try...
Code: [Select]
fwrite(&userpoint, sizeof(struct point), 1, file);
Secondly your function declaration is flawed in the header...
Code: [Select]
struct point addpoint(FILE );Should be...
Code: [Select]
struct point addpoint(FILE* file);
Finally .. I noticed you have the return value of addpoint commented out... it's likely the compiler will complain about that once you fix the other issues.
 
====
Also as general advice, it's a bad idea to open a disk file and keep it open any longer than it needs to be.  A far better strategy would be to get the filename from your user... get the point data into the struct... then write a separate function in which you pass in the name of the file and the address of the struct ... In that function, open the file, write the data and immediately close the file. 
 
One thing you will learn... never trust I/O operations... you need to error check them, get them overwith as quickly as possible and repeat them only as necessary. 
 

Langer.197

  • Guest
Re: Problem with file handling
« Reply #4 on: January 04, 2013, 05:38:51 PM »
Hey Tater,
it worked in a way that after changing what you have said and also "addpoint(*file)" to "addpoint(file)" the compiler wasnt complaining any more and created
a new file containing lots of numbers. Guess thats what it is supposed to do.
Now for me time to move on with the "fread" part.
Thanks a lot for the help and the advice. Since i am a beginner i am always happy when sth works that i dont care that much about how its done (sadly), but hopefully that will change as i get better
and i am getting a better picture of what i am actually doing.
Anyways thanks again. :)

CommonTater

  • Guest
Re: Problem with file handling
« Reply #5 on: January 04, 2013, 06:39:00 PM »
Thanks a lot for the help and the advice. Since i am a beginner i am always happy when sth works that i dont care that much about how its done (sadly), but hopefully that will change as i get better
and i am getting a better picture of what i am actually doing.

Are you taking courses, working from books, etc ?
 
As a beginner this is when you should care most about how stuff is done.  That knowledge will not come later, or by itself and the result is usually a less than skilled programmer.  No offense is intended.  I'm just trying to point you in the right direction...
 
Give this a read...  http://forum.pellesc.de/index.php?topic=4644.msg17797#msg17797  I'm sure you'll find it helpful.
 
 
 

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Problem with file handling
« Reply #6 on: January 04, 2013, 06:49:35 PM »
Thanks a lot for the help and the advice. Since i am a beginner i am always happy when sth works that i dont care that much about how its done (sadly), but hopefully that will change as i get better
and i am getting a better picture of what i am actually doing.
Well, I think this is likely to be your main obstacle when trying to learn how to program.
Rather take smaller steps and make sure you understand each of them before moving on. You are less likely to pick up some bad habits/ideas, just because you didn't care about "how they are done" the first time around...

Ralf

Langer.197

  • Guest
Re: Problem with file handling
« Reply #7 on: January 04, 2013, 07:29:03 PM »
You guys are right. I am old enough to confess that. ;)
The good thing is that two days ago i also realized that.
Now theres a little bit to catch up to.

Thanks for the feedback.

CommonTater

  • Guest
Re: Problem with file handling
« Reply #8 on: January 04, 2013, 08:42:01 PM »
Thanks for the feedback.

No worries... always glad to help a newbie :D  (I still remember my newbie days with a mix of wonder and horror...)

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Problem with file handling
« Reply #9 on: January 04, 2013, 08:46:14 PM »
You guys are right. I am old enough to confess that. ;)
The good thing is that two days ago i also realized that.
Now theres a little bit to catch up to.

Thanks for the feedback.
Gern geschehen!  ;)

Ralf  8)