Opening FD and append Data


 
Thread Tools Search this Thread
Top Forums Programming Opening FD and append Data
# 1  
Old 11-18-2012
Opening FD and append Data

Hello everybody I'am trying to open a File with an Filedeskriptor. After Opening the file I want to append Data to the File!

I have the following code now, but I only overwrite the data from the file and did not append it!

Code:
void Buffer::writeIntoFile(std::string name, int length, std::string text){
    int fd;
    FILE *fp;
    std::ofstream ofs;
    if((fd = open(name.c_str(),(O_CREAT|O_WRONLY|O_RDONLY))) < 0){
      cout << "Error Opening File\n";
    }
    if((fp = fdopen(fd,"a")) < 0){
      cout << "Error\n";
      close(fd);
    }
    //Create a Filebuffer from File
    __gnu_cxx::stdio_filebuf<char> fb(fp,std::ios::out);
    ofs.std::ios::rdbuf(&fb);
    ofs << text<< endl;
    ofs.close();
}


I hope somebody please could help me!
# 2  
Old 11-18-2012
Quote:
Originally Posted by pk543450
Hello everybody I'am trying to open a File with an Filedeskriptor. After Opening the file I want to append Data to the File!

I have the following code now, but I only overwrite the data from the file and did not append it!

Code:
void Buffer::writeIntoFile(std::string name, int length, std::string text){
    int fd;
    FILE *fp;
    std::ofstream ofs;
    if((fd = open(name.c_str(),(O_CREAT|O_WRONLY|O_RDONLY))) < 0){
      cout << "Error Opening File\n";
    }
    if((fp = fdopen(fd,"a")) < 0){
      cout << "Error\n";
      close(fd);
    }
    //Create a Filebuffer from File
    __gnu_cxx::stdio_filebuf<char> fb(fp,std::ios::out);
    ofs.std::ios::rdbuf(&fb);
    ofs << text<< endl;
    ofs.close();
}


I hope somebody please could help me!
If you want a stdio stream, just use:
Code:
fp = fopen(name, "a");

If you want a file descriptor with the same access that the fopen(xxx, "a") provides and you know that the named file already exists, use:
Code:
fd = open(name, O_WRONLY | O_APPEND);

If you want the file open for reading and writing in append mode, that would be:
Code:
fp = fopen(name, "a+");

or:
Code:
fd = open(name, O_RDWR | O_APPEND);

Note that according to the standardsO_RDONLY | O_WRONLY produces undefined results and need not be the same as O_RDWR.

If you don't know if the file already exists and want to create the file if it doesn't exist, then you can add O_CREAT to your open() flags, but you will also need to add a third argument specifying the mode.
# 3  
Old 11-18-2012
Thank you for your very very Helpful Respone!
! THANKS !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append code in a file without opening it

Hi, is it possible to append some codes in a file without opening it. Basically i am working on a script that will add a variable's value in a file (contained within a directory). (2 Replies)
Discussion started by: mukulverma2408
2 Replies

2. UNIX for Dummies Questions & Answers

Append data 1 table to other

Hi. I have 2 create 2 temporary tables.the data will be same with same cols..but after creating 2 tables..i have to merge data in file and send..however the query is after merging data no duplicates shud be present..and only 1 record for a entity must be present.. for eg: table1 has foll cols... (3 Replies)
Discussion started by: musu
3 Replies

3. Programming

Append data to smallint data in informix4gl?

Hi, I have an smallint variable, say "a", i would like to prefix it with "0" in certain conditions. Is it possible to achieve that with this datatype? For instance, a=9 --> a=09 Many thanks (1 Reply)
Discussion started by: dvah
1 Replies

4. Shell Programming and Scripting

want to append the data in one file to the another

Hi , i have two log files, i need to combine this as a one log file. i need to do this by SED , test1.log sadadadaadfsaf test2.log adadadadadada i need this in a single file from test 1 to test2.log test2.log(expected result) adadadadadada (7 Replies)
Discussion started by: mhdmehraj
7 Replies

5. Shell Programming and Scripting

append data to each line

Hi guys, I need to investigate a memory leak on a solaris server, so what I have done is pmap'd each process on the system with a script which tar'd the directory every hour in cron. Now I need to write a script to process the pmap data. So what I have is about 100 directories # ll... (2 Replies)
Discussion started by: borderblaster
2 Replies

6. Shell Programming and Scripting

Append the data to first column

Hi, The below is the content of the file. 008.03.50.21|ID4|0015a3f01cf3 008.04.20.16|ID3|0015a3f02337 008.04.20.17|ID4_1xVoice|00131180d80e 008.04.20.03|ID3_1xVoice|0015a3694125 008.04.30.05|ID3_1xVoice|0015a3f038af 008.06.30.17|ID3_1xVoice|00159660d454... (2 Replies)
Discussion started by: ravi_rn
2 Replies

7. Shell Programming and Scripting

how to append line of of data to file

hai..i am new to unix..and i've currently learn shell script.. i have this small problem where i would like to save every data from log file into user directory if the data is equal to the name of the user.. i manage to do that with below script.. i would like to ask if there is any solutions so... (1 Reply)
Discussion started by: meggae
1 Replies

8. UNIX for Dummies Questions & Answers

append data to file

i want to develop a script newdata that writes new data to a file called items the file items has the following headings columns separated by tabs: channel date time programe if i type executable file newdata on the command line with parameters, it should append it to the items files the... (1 Reply)
Discussion started by: fletcher
1 Replies

9. Shell Programming and Scripting

get the data from sybase and append to the file

How to get the data from the sybase database and append the data obtained into the file. For example, I will get the filename 'temp' from the database. This filename is associated with the number 1.6... This number 1.6 needs to be copied into the file that matches the filename in the... (1 Reply)
Discussion started by: vinay123
1 Replies

10. UNIX for Dummies Questions & Answers

Opening Files in append mode

Is there any other way to open a file in append mode without using C calls or >> redirection? (1 Reply)
Discussion started by: vidhya
1 Replies
Login or Register to Ask a Question