can this be done with file operations

This is a discussion on can this be done with file operations within the C Programming forums, part of the General Programming Boards category; I have a following piece of code for writing to file: Code: #include "stdio.h" #define MAXITEMS (10) typedef struct{ int ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    can this be done with file operations

    I have a following piece of code for writing to file:

    Code:
    #include "stdio.h"
    
    #define MAXITEMS	(10)
    
    typedef struct{				 
    		int     	   eItem;	        //Item id
    		int eDwnlditemsSt;	//Item Download status
    		bool           Dwnldreq;		//Is download req 
    		char 		   pUrl[255];			//Download Url 
    		char           pathstored[255];     // path were it is stored
    		int  offset;		    //offset for next based on item state 
    }tsClnItmsDetails;
    
    tsClnItmsDetails ListOfItemsWrite[MAXITEMS];
    //tsClnItmsDetails ListOfItemsRead[MAXITEMS];
    
    FILE *fmineptr;
    
    void WriteToFlash(FILE *fptr)
    {
    //Can this be done:
    	for(int i = 0; i<MAXITEMS; i++)
    	{
    		fwrite(ListOfItemsWrite[i],1, sizeof(tsClnItmsDetails),fptr);
    	}
    }
    
    
    
    
    int main()
    {
    	int i;
    
    	fmineptr = fopen("mine.txt","w+");
    	if(fmineptr == NULL)
    		printf("Error\n");	
    
    	for(i=0;i<MAXITEMS;i++)
    	{
    		ListOfItemsWrite[i].Dwnldreq = 1;
    		ListOfItemsWrite[i].eDwnlditemsSt =2;
    		ListOfItemsWrite[i].eItem =3;
    
    		ListOfItemsWrite[i].offset=4;
    		ListOfItemsWrite[i].pathstored = "Some path";
    		ListOfItemsWrite[i].pUrl = "My path";
    
    		WriteToFlash(fmineptr);
    	}
    
    	fclose(fmineptr);
    	return 0;
    }
    Q:

    1. I have errors while compiling:
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>d:\source\trystructread\trystructread\trystructr ead\trystructread.cpp(52) : error C2440: '=' : cannot convert from 'const char [10]' to 'char [255]'
    1> There is no context in which this conversion is possible
    1>d:\source\trystructread\trystructread\trystructr ead\trystructread.cpp(53) : error C2440: '=' : cannot convert from 'const char [8]' to 'char [255]'


    2. I am have to write an entire structure to a File.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Yes, it can be done, but you're not doing it right. Doesn't "fwrite()" take a pointer as the data? I don't see your code passing a pointer.
    And, you can't copy two arrays by just using "=". Use "strcpy()" instead.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Quote Originally Posted by GReaper View Post
    Yes, it can be done, but you're not doing it right. Doesn't "fwrite()" take a pointer as the data? I don't see your code passing a pointer.
    And, you can't copy two arrays by just using "=". Use "strcpy()" instead.
    I am passing the baseAddress to fwrite();

    Kindly help me with a compilable statement for the above fwrite.


    Thanks in advance

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Have you fixed your string "assignments" yet - the actual error messages you posted.

    Also, look at the loop in main - you're writing the WHOLE file for every loop iteration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performing File operations using File Inode number
    By rak1986 in forum C Programming
    Replies: 4
    Last Post: 09-22-2008, 02:43 AM
  2. Help with File operations
    By shwethu in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 05:52 AM
  3. File operations
    By coo_pal in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 02:01 AM
  4. File operations?
    By papudi in forum C Programming
    Replies: 11
    Last Post: 05-17-2002, 09:10 PM
  5. File Operations (2)
    By cnealcoc in forum C Programming
    Replies: 2
    Last Post: 04-30-2002, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21