Malloc and File Creation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Malloc and File Creation
# 1  
Old 01-26-2011
Malloc and File Creation

How can I use malloc with copying/creating files?

Is this the correct way?

I'm a bit confused...

Code:
   int in_fd;
   int *out_fd;
   char buffer;



   in_fd = open(av[1], O_RDONLY);
   out_fd = malloc(strlen(av[1])+strlen(av[2])+2);
   sprintf"(buffer,%s/%s", av[2],av[1]);

# 2  
Old 01-26-2011
Sorry, could you give explanation of what it is you are trying to do ?
Are you trying to code a program to copy one file to another ?
If so, then usually you probably wouldnt bother malloc'ing memory - you could simply use a buffer allocated on stack of a given size, eg "char buffer[BUFSIZ];" and then read into and write from that buffer multiple times until the whole file has been copied. Obviously that buffer could be malloc'ed, but most people dont bother as you then have to handle a NULL return from malloc, and error handling, etc.

I hope this helps...
# 3  
Old 01-26-2011
Quote:
Originally Posted by l flipboi l
How can I use malloc with copying/creating files?
malloc has nothing to do with files.

Pretty sure he's trying to allocate memory to hold a string, like so:

Code:
// Enough mem to hold both strings, and '/', and the NULL terminator
char *buffer=malloc(strlen(av[1]) + strlen(av[2]) + 2);
sprintf(buffer, "%s/%s", av[1], av[2]);

printf("buffer is %s\n", buffer);

...

// once you know for sure nothing will ever use buffer anymore
free(buffer);

# 4  
Old 01-26-2011
Does malloc allocate space for output files? That's what I had in mind, I was trying to dynamically allocate space from a read input file. It seems that malloc would be used only to create pathnames????
# 5  
Old 01-26-2011
Quote:
Originally Posted by l flipboi l
Does malloc allocate space for output files?
malloc allocates memory. You can use it for whatever you want.

I don't understand what you mean by 'for output files'. Could you tell me what you're actually trying to do?
# 6  
Old 01-26-2011
sorry, I think i'm getting things mixed up.

I understand what mallocs does now. Thank you!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print the specific part of the file name with file creation date?

Hello Folks, I have an requirement, where i need to get total count of the file based on creation date with there filename selected pattern. Filename: MobileProtocol.20171228T154200.157115.udr I want to get the count of files created on each day based on a pattern find. find . -type... (7 Replies)
Discussion started by: sadique.manzar
7 Replies

2. Programming

Malloc problem with fread() to read file to structure in C

Hello, I am trying to read a text file into linked list, but always got the first and last records wrong. 1) The problem looks related to the initialization of the node temp with malloc(), but could not figure it out. No error/warning at compiling, though. 2) The output file is empty,... (10 Replies)
Discussion started by: yifangt
10 Replies

3. Shell Programming and Scripting

File creation

Hi I need to write a file with value value from Paramer which is passed from datastage. It should overwrite the file if already exists. Can anybody provide command for this? (1 Reply)
Discussion started by: cnrj
1 Replies

4. Shell Programming and Scripting

Help with creating a text file in perl with file creation date.

Hi, I am quite new to Perl scripting and i need to create a .TXT file using perl, with fields (A,B,C,D,E), and this text file should be named with current file creation date "XYZ_CCYYMMDD.TXT" (i.e.XYZ_2011042514:33 PM). Can anyone who has done this, please share their expertise on this... (5 Replies)
Discussion started by: msrahman
5 Replies

5. Solaris

gzip a file and append creation date stamp to file

I want to gzip a file and append the creation date to the end of the file. How can I accomplish this task. Basically they are log files which need a creation date stamp appended to make sure they do not overwrite other log files. -jack (3 Replies)
Discussion started by: jacktravine
3 Replies

6. UNIX for Advanced & Expert Users

file creation

Hi, Is there any way to restrict directories with one type of file creation. regards. (8 Replies)
Discussion started by: guguli
8 Replies

7. Shell Programming and Scripting

Excel file creation

Hi, I want to create.xls file using unix command. I know how to generate csv file. But in some case i want to insert some rows in the excel file without affecting its styles(like bold, border etc.) Thanks (0 Replies)
Discussion started by: lathish
0 Replies

8. Shell Programming and Scripting

file creation

hi guys Kindly see the below script #!/bin/bash if then # check to see if file notrouter exits or not if ! ls -l notrouter /root/joy/ >/dev/null then touch /root/joy/notrouter else echo "Entries already exist" fi else echo "Entries does not exist" fi here... (1 Reply)
Discussion started by: whizkidash
1 Replies

9. Linux

creation of a file

how to create a file with the same modification time as another file is having using the copy command? (1 Reply)
Discussion started by: infyanurag
1 Replies

10. UNIX for Advanced & Expert Users

Get data creation file

Hi, I've two machine A and B. On the machine B there's a script that get with an ftp command a file on the A machine. I want that creation data file on the machine B is the creation data file on the machine A. Example: File text.txt on machine A created on 01/01/2006 11:00. The script on the... (1 Reply)
Discussion started by: superfabius
1 Replies
Login or Register to Ask a Question