Results 1 to 4 of 4
Hi,
I have written a simple C program in Ubuntu 10.10 get editor. The text of the code is shown below.
When I apply the combined gcc compile and link ...
- 03-23-2011 #1Just Joined!
- Join Date
- Mar 2011
- Posts
- 3
Help with a specific C program in Ubuntu
Hi,
I have written a simple C program in Ubuntu 10.10 get editor. The text of the code is shown below.
When I apply the combined gcc compile and link command in the terminal to the file which contains the code below I get the following errors:
Errors
kopy_1.c: In function ‘main’:
kopy_1.c:34: error: ‘uppercase’ undeclared (first use in this function)
kopy_1.c:34: error: (Each undeclared identifier is reported only once
kopy_1.c:34: error: for each function it appears in.)
kopy_1.c:34: error: ‘var’ undeclared (first use in this function)
Can anyone provide some assistance to solve these errors so that I can get the command to work successfully?
Code
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main (argc, argv)
int argc;
char *argv[];
{
int i, j ;
int fileArg = 0 ; // Count for files on command line
int infd, outfd ; // File descriptor for input and output file
int num_read, num_write ; // Numbers of characters returned by a file access
char buff [1024] ; // Buffer for use in file access
char* fileName[2] = {} ; // Point to file names
// Parse the command line arguments
for ( i = 1 ; i < argc; i++ )
{
if (argv[i][0] == '-') // Check for a command option
// Simple message display, a real system would process options
printf ("You have selected an option\n");
else fileName[fileArg++] = argv[i];
}
// If uppercase_var is set then convert all buff's characters to uppercase
if(uppercase-var == 1)
{for(i=0; i<sizeof(buff); i++)
if(islower(buff[i]))
buff[i] = toupper(buff[i]);
}
// Open the first file for reading
infd = open (fileName[0], O_RDONLY, 0600 );
if (infd == -1)
{ printf ("Failed to open first file %s\n", fileName[0]);
return 1;
}
// Open/create the second file for writing
outfd = open (fileName[1], O_CREAT | O_WRONLY, 0600 );
if (outfd == -1)
{ printf ("Failed to open second file %s\n", fileName[1]);
close (infd);
return 1;
}
// Now copy the contents of the first file to the second file
while ((num_read = read (infd, buff, sizeof (buff))) > 0)
{
num_write = write (outfd, buff, num_read);
// Check for basic failure
if (num_write != num_read)
{
printf ("Problem - mismatch between write size and read size!\n");
}
}
// Remember to close all open files before exit
close (outfd);
close (infd);
return 0 ;
}
- 03-23-2011 #2
You wrote "uppercase-var" instead of uppercase_var. That subtracts an undeclared variable "var" from an undeclared variable "uppercase". Hence the errors.
ps Now I look again and I don't think you declared uppercase_var either!"I'm just a little old lady; don't try to dazzle me with jargon!"
- 03-23-2011 #3Just Joined!
- Join Date
- Mar 2011
- Posts
- 3
- 03-24-2011 #4
You declare it near the top where you've declared your other variables. The simplest way would be to declare it as an integer: int upper_var. You might want to set a default value: int upper_var=0.
A more elegant way would be to define a boolean type with permissible values TRUE and FALSE:
(I leave it to you to find out why FALSE and TRUE have to be given in that order)Code:typedef enum boolean {FALSE, TRUE}; boolean upper_var=FALSE;
But declaring a variable is only the beginning. You also need to give it a value. Where does your upper_var get its value from? Is it a command line option to the program, something like "-U"? If so, you'll need to go through the argv array (there's a function called getopts that will do that for you) and set upper_var if that option is found; that's why it's useful to have it unset by default.
The key to programming is to think logically and systematically."I'm just a little old lady; don't try to dazzle me with jargon!"


Reply With Quote
