LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2006, 10:09 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Writeing block data to a file.


Im writing a simple program that will write data into a random access file from a structure containing the data.

im having a bit of an issue with the fwrite function and have read the man page and
still having no luck. Im thinking it may be my logic.

here is the function that does all the nifty stuff.

Code:
 
  void addaccount(void)
{
  FILE *datafile; 
  ACCOUNT newacct; 
  char grabed[40];
  int tempdata; 
   
  
  datafile = fopen("data.dat","w"); 
  if (datafile == NULL) 
     printf("Cannot open file for write.  May be a permissions issue.\n"); 
  else 
    {
      printf("Please enter the account number to add\n:>"); 
      scanf("%s",grabed);
      sscanf(grabed,"%d",&tempdata); 
      newacct.accnum = tempdata; 
      
      printf("\nPlease enter the account holders first and last name\n:>"); 
      scanf("%s%s",newacct.first,newacct.last); 
      printf("\n"); 
      
      fwrite(newacct,sizeof(ACCOUNT),1,datafile); 
      
      printf("Account information written to file\n"); 
   } 
}

from what I gather fwrite requires there be a const ptr used to write to the stream pointed by FILE * . I thought when you create a structure AKA intilaize it what it does is make the name you give it a pointer to the structure data in memory. but gcc is telling me its incompatible. Now im probably wrong with my implementation here but i was sure there was a way to do this without having to use malloc and create a void pointer to sizeof(struct name) or is that how you do it and im just following a red herring ?
 
Old 05-12-2006, 10:34 PM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Oops found out what my issue was

not passing address of the struct. had to use a & on there.

sorry for the post.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
when inodes, superblock and data block are created? xcore_on Linux - Newbie 1 05-02-2006 05:54 PM
regarding file system block size rajesh_b Programming 0 03-24-2006 12:31 AM
Need help with writeing a new system call! What is a file descriptor? draksonije Programming 3 03-22-2006 03:08 AM
writeing a floppy image sethgeekx86 Linux - Newbie 3 02-14-2004 12:28 PM
Block FTP-DATA Port naveedpta Linux - Security 6 11-22-2003 09:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration