fscanf seg fault

This is a discussion on fscanf seg fault within the C Programming forums, part of the General Programming Boards category; It's another part of the assignment. Most stuff online seems to be giving out that open etc aren't real C, ...

  1. #16
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    It's another part of the assignment.

    Most stuff online seems to be giving out that open etc aren't real C, rather than telling me how to use them compared to the others!

    I'm working through the code trying to chance them all appropriately.

    This is what I have thus far... it compile at least, but doesn't seems to work!

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    #define STR(arg) #arg
    
    #define BUF_SIZE 80
    
    #define BUF_FORMAT "%80[^\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);
    		}
    	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;
    			
    		
    	
    	
    			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;		
    		}
    	}
    Last edited by mercuryfrost; 08-20-2009 at 12:17 PM. Reason: Update

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by mercuryfrost View Post
    It's another part of the assignment.

    Most stuff online seems to be giving out that open etc aren't real C, rather than telling me how to use them compared to the others!

    I'm working through the code trying to chance them all appropriately.

    This is what I have thus far... it compile at least, but doesn't seems to work!
    Probably because they're not. They're UNIX system calls. If you are on a *nix machine, then you can use them; but notice that they don't use FILE* at all, but rather Unix file descriptors (also known as "int"s). Type "man open", "man write", "man <whatever>" at your terminal for information about them.

  3. #18
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    @tabstop.

    That seems to open a man for other things.
    Open gives man page for openvt, write for messages to users etc...


    As regards the program above,
    It seems to be compiling fine and running fine.

    It creates the output file fine, but the permissions on the file seem to be random.
    EDIT: Didn't set permissions after O_CREAT. So program was using random stack values to set permission.

    It's not actually reading the input file right I don't think...

    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;		
    		}
    	}

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You may need "man 3 open" etc. (EDIT: or "man 2 open".)

  5. #20
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    @ Tabstop, that doesn't seem to work either.

    Actually, since it's a different problem,
    I'll start a new thread.

    The original issue in this thread was solved with all of your help, thanks!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf causes a SEGMENTATION FAULT
    By yougene in forum C Programming
    Replies: 15
    Last Post: 12-29-2008, 12:11 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 02:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 09:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 04:24 PM

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