C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-20-2009, 01:28 PM   #1
Registered User
 
Join Date: Aug 2009
Posts: 18
open read write close

As a progression from the first thread,
which was solved, using fread fscanf fprintf and fclose
(fscanf seg fault)

I'm trying to write the same program,
but in this assignment, I must not use any of the above calls,
but instead use open, read, write, and close.


My code so far compiles fine,
and seems to run, it creates or opens the output file fine,
but doesn't append the input files to it.

I think it's a problem with the read call,
but as I said, I'm new to C!

Any ideas welcome

Code:
#include <stdio.h>
#include <fcntl.h>
#define STR(arg) #arg

#define BUF_SIZE 160

#define BUF_FORMAT "%160[^\n]s"

char buf[BUF_SIZE];
FILE *output, *input, *buffer;									/* declarations*/

int i,j,openstatone,openstattwo;



int main (int argc, char *argv[])
	{
        if ( argc < 2 ) 								/* Error message if no Args entered */
		{
		write(1,  "\n **************** \n Not enough Arguments Supplied\n ****************\nPlease enter an Output file to append to, followed by any input files\n \n",  142);
		
		return 1;
		}	
	else  										/*Output file opened or created */
		{
		openstatone = open(argv[1], O_CREAT, 0777);
		}
	if (openstatone > 0)
		{

		if (argc == 2)									/*If only one Arg entered*/
			{
			write (1, "No input files appended to Output File.\n", 40);			
			return 0;
			}
		else										/* start append section */
			{
			i = argc -2;
			j = 2;
			printf ("i = %d\n", i);
			printf ("j = %d\n", j);
		
	
	
			while (i > 0)								/*Loop through all input files*/
				{
				/*printf ("%s\n", argv[j]);*/					/* Used for Error Checking*/
				openstattwo = open(argv[j], O_RDONLY);

				if (openstattwo > 0)						/*Check fopen worked */
					{
					read (argv[j], buf, 160);
					printf("%s\n", buf);    				/* Used for Error Checking */
					write (argv[1], "%s\n", buf);						
  					close(argv[j]);
					write (1,"SUCCESS! Input File: %s was Successfully appended\n", argv[j], 80);				
					}
				else
					{
					write (1, "FAILURE! Input File: %s Failed to Open or does not exist\n", argv[j], 80);
					}
				j ++;
				i --;
				}
			write (1, "\nFiles Appended Successfully\n\n", 80);				/*Files Appended Successfully*/
			close(argv[1]);			
			return 0;
			}
		}
	else
		{	
		/*write (1, "Output file failed to Open", 80);*/
		return 1;		
		}
	}
mercuryfrost is offline   Reply With Quote
Old 08-20-2009, 01:30 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
If you want to append, why not use O_APPEND?
tabstop is offline   Reply With Quote
Old 08-20-2009, 01:34 PM   #3
Registered User
 
Join Date: Aug 2009
Posts: 18
Well I thought that O_CREAT would create the file and open it. Or just open it if it already exists.

O_APPEND wouldn't open it if the file didn't exist would it?

Well that was my thinking anyway!
mercuryfrost is offline   Reply With Quote
Old 08-20-2009, 01:40 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,862
You're thinking that these flags are mutually exclusive. They are not. You are supposed to | together the ones you want.
tabstop is offline   Reply With Quote
Old 08-20-2009, 02:00 PM   #5
Registered User
 
Join Date: Aug 2009
Posts: 18
Ah okay,
Changes made.

Still the same problem persists. The input files input isn't being read

I think the problem is with the read statement...

Tidied a bit

Code:
#include <stdio.h>
#include <fcntl.h>
#define STR(arg) #arg

#define BUF_SIZE 160

#define BUF_FORMAT "%160[^\n]s"

char buf[BUF_SIZE];
FILE *output, *input, *buffer;									/* declarations*/

int i,j,outfp,infp,fileread;



int main (int argc, char *argv[])
	{
        if ( argc < 2 ) 								/* Error message if no Args entered */
		{
		write(1,  "\n **************** \n Not enough Arguments Supplied\n ****************\nPlease enter an Output file to append to, followed by any input files\n \n",  142);
		
		return 1;
		}	
	else  										/*Output file opened or created */
		{
		outfp = open(argv[1], O_CREAT | O_APPEND, 0777);
		}
	if (outfp > 0)
		{

		if (argc == 2)									/*If only one Arg entered*/
			{
			write (1, "No input files appended to Output File.\n", 40);			
			return 0;
			}
		else										/* start append section */
			{
			i = argc -2;
			j = 2;
			printf ("i = %d\n", i);
			printf ("j = %d\n", j);
			
			while (i > 0)								/*Loop through all input files*/
				{
				/*printf ("%s\n", argv[j]);*/					/* Used for Error Checking*/
				infp = open(argv[j], O_RDONLY);

				if (infp > 0)						/*Check fopen worked */
					{
					printf ("testing 1\n\n\n");
					
					fileread = read (infp, buf, 160);
					printf("%s\n", buf);    				/* Used for Error Checking */
					write (outfp, buf, 160);						
  					close(infp);
					write (1,"SUCCESS! Input File: %s was Successfully appended\n", argv[j], 80);				
					}
				else
					{				
					write (1, "FAILURE! Input File: %s Failed to Open or does not exist\n", argv[j], 80);
					}
				j ++;
				i --;
				}
			write (1, "\nFiles Appended Successfully\n\n", 80);				/*Files Appended Successfully*/
			close(outfp);			
			return 0;
			}
		}
	else
		{	
		/*write (1, "Output file failed to Open", 80);*/
		return 1;		
		}
	}

Last edited by mercuryfrost; 08-20-2009 at 02:19 PM. Reason: update
mercuryfrost is offline   Reply With Quote
Old 08-21-2009, 03:55 PM   #6
Registered User
 
Join Date: Sep 2008
Location: Toronto, Canada
Posts: 525
What is this stuff:
Code:
read (argv[j], buf, 160);
printf("%s\n", buf);    /* Used for Error Checking */
write (argv[1], "%s\n", buf);
read(), write() and close() expect a file handle... not some string as its first argument.
Also, you can't use formatters.

Last edited by nonoob; 08-21-2009 at 03:57 PM.
nonoob is offline   Reply With Quote
Old 08-22-2009, 08:09 AM   #7
Registered User
 
Join Date: Aug 2009
Posts: 18
I think I had already changed that when you had posted.

Did I?

Or is it this you're referring to?
Code:
infp = open(argv[j], O_RDONLY);
If that won't work, any suggestions?
mercuryfrost is offline   Reply With Quote
Old 08-23-2009, 05:27 PM   #8
Registered User
 
Join Date: Sep 2008
Location: Toronto, Canada
Posts: 525
No, the open() looks OK, argument-type-wise.
nonoob is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read from txt and write in an external program sombrancelha C Programming 8 06-01-2009 09:04 PM
file won't close despite calling the function TiffClose(image) caggles C Programming 1 05-27-2009 12:46 PM
I was wondering if anyone could help me with a read and write function Wolfman C Programming 6 04-17-2009 04:41 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Ghost in the CD Drive Natase A Brief History of Cprogramming.com 17 10-12-2001 05:38 PM


All times are GMT -6. The time now is 01:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22