GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 3.67 average.
  #1  
Old 23-Dec-2007, 06:26
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

Using FILE pointer in C++


I have a function which is a member of a class.
In the function, the standard C- File pointer FILE is used to write into a text file.

if i inherit that class in my local class and call the function to write into a file, the code is compiling fine but i am getting segmentation fault while executing .

My doubt is can we use a FILE pointer in the C++ code or should i take any special cares for such situations.

The problem is the function that writes the file is a library function not a *.cpp file so i cant change the FILE pointer to standard STL fstream pointer.

Can any one help me out of this?
  #2  
Old 23-Dec-2007, 06:33
inevitable inevitable is offline
Junior Member
 
Join Date: Nov 2007
Posts: 53
inevitable is on a distinguished road

Re: using FILE pointer in C++


Yes, you can use FILE * in c++ class, there is nothing wrong in inheriting the base class that as FILE*.

To clarify more , you can post your code, we can give you the right reply why the code is causing segmentation fault.
Becuase segmentation fault may be because of some other invalid pointer access.
  #3  
Old 23-Dec-2007, 10:29
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

Re: using FILE pointer in C++


You should be able to use any C in C++ code.

My guess is you're not initializing the FILE * in the derived class like you are in the base class. Like inevitable said, post some code and we can help further.
  #4  
Old 23-Dec-2007, 22:36
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

Re: using FILE pointer in C++


Thank you very much for your support .
The problem was not there in the FILE pointer after more debugging, i found out that the class contains a structure as a member which was not allocated memory . I allocated memory for that and it is working fine.

But, now the problem is all the data what i want to write into the file is in a map<string,string>mapTemp; But these data should be updated to the protected members of a class

Below is the example code:

CPP / C++ / C Code:
class Test{
             public : 
                       void SetDesign(char *s){DesignName= s;}
                       void SetLotNum(char *s){LotNum = s;}
                       void WriteFile(char *);
                protected :
                     char *DesignName;
                     char *LotNum;
                      char *...
                            ...
                            ...
          };

int main()
{
    map<string,string>mapTemp;
     
    mapTemp["DESIGN"] = "Design";
    mapTemp["LOTNUM"] = "LotNum";
    mapTemp["PRODUCTNUM"] = "ProductNum";

  for(It = Temp.begin();It != Temp.end(); It++)
   {
        if (It->first == "DESIGN")
	{
          pcTemp = new char [It->second.size() +1];
	  //strcpy(pcTemp,It->second.c_str());
	  strcpy(pcTemp,It->second.c_str());
          objRead.SetDesign(pcTemp);
	  delete [] pcTemp;
	 }
        if (It->first == "LOTNUM")
	{
          pcTemp = new char [It->second.size() +1];
	  strcpy(pcTemp,It->second.c_str());
	  //strcpy(pcTemp,"Haidesi");
          objRead.SetLot(pcTemp);
	  delete [] pcTemp;
          }

        WriteFile("Hi.txt");
  return 1;
 }

Where WriteFile opens the file through FILE pointer (which is a library) and writes into the file.

I am able to Generate the File but the values written into the file has only one value as the "VALUE" in the map<KEY,VALUE> pair.

i.e, in my example i have three keys and 3 values (DESIGN,LOTNUM,PRODUCTNUM) as KEYS and corresponding values.

In the map "PRODUCTNUM" is the last key, so it is the las one that is processed in the for loop, while processing, all the member variables are initialised with the VALUE of this last <KEY, VALUE > pair i.e, "PRODUCTNUM"

So only the "ProductNum" is getting printed .

But, if i initialise the variables by calling the functions(SetDesign,SetLotNum..)

outside the for loop, the file is generating fine.
I am not understanding where i am going wrong in the conversion of string to char can some one help me out of this.
Last edited by admin : 23-Dec-2007 at 23:27. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #5  
Old 24-Dec-2007, 08:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,153
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: using FILE pointer in C++


Quote:
Originally Posted by vicky_brsh
...I am not understanding...

Since I don't have enough code to actually compile and test, I can't really tell you whether you are "going wrong" in more than one place, but here's one observation:


In the first part of the loop you allocate a block of memory and copy "DESIGN" into that memory with strcpy(). So far, so good.

You pass the address of that block to the SetDesign() function. Sounds reasonable.

In the SetDesign() function you set the value of the DesignName member element (a pointer to char) to the address of the block of memory to which you have copied "DESIGN". Still OK.

Then, back from SetDesign(), you de-allocate that memory block. Oops.

Beyond this point, it is illegal to dereference that memory any more, since your program no longer "owns: it. In other words the DesignName member points to an invalid memory address. Any access to DesignName will result in undefined behavior. The program may appear to "work." The program may crash. Some parts of the program may appear to "work," and others may not. The program may "work" sometimes and crash sometimes. The program may not crash, but give erratic results (like it goes into never-never land). That's what "undefined behavior" means.

Etc.

Regards,

Dave

Footnote: Have you considered using std::string objects instead of pointers to chars for DesignName, LotName, etc.? Most of these considerations would go away. No need to allocate and de-allocate memory. Simple copy with an assignment statement instead of a C library function. Etc., etc.

Sometimes there is a good reason to use arrays of char and pointers to char in C++ programs instead of using std::strings. I don't see it for this program. (But that's just me. I don't know what your actual requirements are.)
  #6  
Old 26-Dec-2007, 03:27
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 25
vicky_brsh has a little shameless behaviour in the past

Re: using FILE pointer in C++


Hi Dave,

Thank you very much for correcting my mistake. Yes you are right in pointing out my mistake.

I should not free the pointer till i call the function to write the file . Now i have taken different pointers and i am deleting the memory allocated afer calling the function to write into the file.

thanks you very much
regards
Vikram
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:08.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.