LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-31-2007, 12:14 AM   #1
Komelore
LQ Newbie
 
Registered: Feb 2007
Distribution: Gentoo
Posts: 10

Rep: Reputation: 0
Quick C question


Hey everyone!
Im trying to write a program in C that makes a copy of another file. Im having problems so far.
Quote:
#include <stdio.h>
main()
{
int c;
FILE *IPFile;
FILE *OPFile;


IPFile = fopen("/home/$USER/Desktop/examplefile.txt","r");
OPFile = fopen("/home/$USER/backup/(randomname)","w");


while ((c = fgetc(IPFile)) != EOF)
{
fputc(c, OPFile);
}

fclose(IPFile);
fclose(OPFile);
}
I would really like to figure out a way to generate random names and use it in place of the actual file name. I know its inefficient but i'm trying to learn. As an example...I dont know why something like this doesnt work
Quote:
int x
x = (some value)
(and then at the OPFILE part...)
OPFile = fopen("/home/$USER/backup/%s",x,"w");
I tried stuff like that with random numbers but it gives me this error:
Quote:
warning: passing arg 2 of `fopen' makes pointer from integer without a cast
error: too many arguments to function `fopen'
Thanks in advance.
 
Old 03-31-2007, 01:41 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you'd use sprintf to convert the integer into a formatted string. eg...

sprintf (filename, "myfile%d.txt", x);
fopen(filename);
 
Old 03-31-2007, 01:44 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you need to convert the integer you had from the random number you generated to a string. You can do this using a function such as sprintf, check out man sprintf...
 
Old 04-02-2007, 03:28 PM   #4
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
By the way, use [ code ] instead of [ quote ] for code samples, to keep the indentation.
 
  


Reply



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
Quick question graystarr Linux - Hardware 2 02-21-2006 04:17 PM
Question Concerning ISO's and one quick question. evrae Linux - Software 2 06-21-2004 03:53 AM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM
Quick Question rhraz Linux - Newbie 2 02-17-2004 09:34 AM
Quick question Nylex Linux - General 8 07-13-2003 03:25 PM

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

All times are GMT -5. The time now is 07:29 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