LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-17-2005, 11:36 AM   #1
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Rep: Reputation: 15
Is this allowed?


I have a small program that I am trying to write. The point of the program is to accept 3 parameters: -h | -b buf | source | target

The -h if active, creates a file with holes
The -b nbuf is supposed to allow the user to enter a specified amount for the buffer when reading and writing to a file.

The source is the source file and the target is a target file. The code that I have so far is as follows. One problem that I am having is that I receive the following errors upon compilation:

my.c: In function `main':
my.c:15: warning: assignment makes integer from pointer without a cast
my.c:19: warning: passing arg 1 of `read' makes integer from pointer without a cast
my.c:19: warning: passing arg 2 of `read' makes pointer from integer without a cast
my.c:32: warning: passing arg 1 of `write' makes integer from pointer without a cast
my.c:32: warning: passing arg 2 of `write' makes pointer from integer without a cast
my.c:33: warning: passing arg 1 of `lseek' makes integer from pointer without a cast

Below is the code that I have written

Quote:
int main ( int argc, char *argv[] )
{
int wfile;

// Set the buffer to argc2
int buffer;
buffer = argv[2];
int count;

// Read the information in file argv3
count = read(argv[3],buffer,1);

if (argv[1] != "-h")
{
// Create the target file. Output error if file cannot be created
if ((wfile = creat(argv[4], S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0)
{ printf("Unable to create target file, check permissions\n");
}

// Read and write all of the contents from source file to the new file
while (count != EOF)
{
// Write the file content
write(argv[4],buffer,1);
lseek(argv[3],count++,SEEK_SET);
}
}
else
{
// Create file with holes
}

exit(0);
}

Last edited by alltime; 02-17-2005 at 12:50 PM.
 
Old 02-17-2005, 12:52 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You'll actually need to open the file first, etc. You can't read directly from a filename. You need to open() the file using the filename. open() will return a file descriptor and you use that file descriptor for doing all the file operations like read()'ing and write()'ing. When you're doing playing with the file you'll need to close() it.
 
Old 02-17-2005, 01:17 PM   #3
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
LOL, thanks I forgot that is required.

If I can open the file with:

int count;
int openfile;

if((openfile = open(argv[3],O_CREAT)) < 0)
error....
else
while (count != EOF)
{
lseek(argv[3],count++,SEEK_SET);
}

argv[3] meaning the source file. I'm a little unclear on pointers actually so how could I use the pointers in the read or write command.

Last edited by alltime; 02-17-2005 at 01:18 PM.
 
Old 02-17-2005, 01:28 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You'd pass openfile to lseek() instead of argv[3].
 
Old 02-17-2005, 01:58 PM   #5
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
Thanks, I appreciate your help.
 
Old 02-17-2005, 02:22 PM   #6
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
The second parameter that the user sends to the program is the buffer that is used to write.

int buffer;
buffer = argv[2];

If I use this in a write function, for example:

write(opentarget,buffer,1);

I receive the error: arg 2 of `write' makes pointer from integer without a cast

I know that if buffer was a char and I was making it equal to a char, I would just have to add & after the = sign. What would be required for this?
 
Old 02-19-2005, 10:39 AM   #7
alltime
Member
 
Registered: Jan 2005
Distribution: Mandrake 10.1 with kernel 2.6.10
Posts: 60

Original Poster
Rep: Reputation: 15
bump
 
Old 02-19-2005, 10:46 AM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
you declared buffer to be a integer and then your trying to assign it a char string from argv

this is your error


also isnet write a low level file operation or am i trippen on somethin ?

I dont remeber much of low level file operations but why is there a 1 in the write function at the end ?

Quote:
I know that if buffer was a char and I was making it equal to a char, I would just have to add & after the = sign. What would be required for this?
HUH????? no that would make it assign right

char buffer

buffer = argv[3] <--- argv[3] is a string

Last edited by exvor; 02-19-2005 at 11:08 AM.
 
Old 02-22-2005, 04:19 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
maybe look at the getopt function
in stdlib.h for parsing command line arguments?

man section 3c on solaris.
 
  


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
Method Not Allowed: The requested method POST is not allowed for the URL /writedhcp.p WiWa Linux - Networking 15 01-06-2011 01:20 PM
(Debian)DirectoryIndex not allowed here MDFH Linux - Newbie 1 02-04-2005 04:38 PM
Are we allowed to sell stuff here? Lurker01 General 3 10-21-2004 01:29 AM
MySQL - Host not allowed TBomb Linux - Software 4 09-18-2004 05:22 AM
2 questions. Is that allowed? That's 3... M-8 Linux - Newbie 2 04-19-2004 04:36 PM

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

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