Error when running program

This is a discussion on Error when running program within the C Programming forums, part of the General Programming Boards category; Below is the code for my program. It does everything I want it to, but then at the end of ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

    Error when running program

    Below is the code for my program. It does everything I want it to, but then at the end of running it an error appears. I can't figure out why it gives an error after doing everything I told it to successfully.

    any help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 100
    
    
    main()
    
     {	
    	int i,j,k,q=0;
    	FILE *fp, *fp2;
    	char s[N]={"\0"}, word2[N]={"\0"}, str[N]={"\0"}, file2word1[N]={"\0"}, file2word2[N]={"\0"};
    	fp = fopen("check.out", "w");
    	gets(s);
    	fprintf(fp, "%s\n", str);
    	printf("Line input has been added to check.out\n");
    	fclose(fp);
    	
    	for(i=0; s[i]!='\0';i++)
    		{	
    			if(s[i]==' ')
    				break;
    		}
    	for(j=i+1, k=0; s[j]!=' '; j++, k++)
    		{
    			word2[k]=s[j];
    		}
    	
    	if(word2[0]=='\0')
    		{
    			printf("There is no second word in the input text.\nFile opening not tried.\n");
    			q++;
    			goto next;
    		}
    	
    		if(q!=1)
    		{
    			if((fp2 = fopen(word2, "r")) == NULL)
    		
    			{
    				printf("Tried opening file '%s'\nFile opening failed - file does not exist.\n", word2);
    				q++;
    			}
    		}
    	next:
    	if(q!=1)
    		{
    		fp2=fopen(word2, "r");
    	
    		fscanf(fp2, "%s", &file2word1);
    		fscanf(fp2, "%s", &file2word2);
    		printf("Tried opening file '%s'\nFile opening successful - %s found and opened.\nSecond word in %s is '%s'", word2, word2, word2, file2word2);
    		}
    	
    	return 0;
     }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What error do you get? That can sometimes be helpful for finding the cause of the problem. Aside from ghastly programming style, the only real problems you should have at runtime (from a quick glance) is this:
    Code:
    fscanf(fp2, "%s", &file2word1);
    fscanf(fp2, "%s", &file2word2);
    Remove the address-of operators. The name of an array is already a pointer.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    Originally posted by Prelude
    What error do you get? That can sometimes be helpful for finding the cause of the problem. Aside from ghastly programming style, the only real problems you should have at runtime (from a quick glance) is this:
    Code:
    fscanf(fp2, "%s", &file2word1);
    fscanf(fp2, "%s", &file2word2);
    Remove the address-of operators. The name of an array is already a pointer.
    It crashes.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    PC-lint for C/C++ (NT) Ver. 8.00m, Copyright Gimpel Software 1985-2003
    
    --- Module:   test.c
    test.c 13: [Warning 534] Ignoring return value of function 'gets(char *)' (compare with line 179, file stdio.h)
    test.c 13: [Warning 421] Caution -- function 'gets(char *)' is considered dangerous
    test.c 14: [Warning 668] Possibly passing a null pointer to function 'fprintf(FILE *, const char *, ...)', arg. no. 1 [Reference: file test.c: line 12]
    test.c 23: [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 18, 23]
    test.c 25: [Warning 661] Possible access of out-of-bounds pointer (1 beyond end of data) by operator '[' [Reference: file test.c: lines 18, 23, 25]
    test.c 23: [Warning 661] Possible access of out-of-bounds pointer (2 beyond end of data) by operator '[' [Reference: file test.c: lines 18, 23]
    test.c 32: [Info 801] Use of goto is deprecated
    test.c 48: [Warning 545] Suspicious use of &
    test.c 48: [Warning 561] (arg. no. 3) indirect object inconsistent with format
    test.c 48: [Warning 668] Possibly passing a null pointer to function 'fscanf(FILE *, const char *, ...)', arg. no. 1 [Reference: file test.c: line 46]
    test.c 49: [Warning 545] Suspicious use of &
    test.c 49: [Warning 561] (arg. no. 3) indirect object inconsistent with format
    test.c 59: [Info 766] Header file 'stdlib.h' not used in module 'test.c'
    test.c 59: [Info 766] Header file 'string.h' not used in module 'test.c'
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I just ran the program and it ran fine as long as it could open the file

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    > gets(s);
    > fprintf(fp, "%s\n", str);
    So you read stuff into s, and print out the empty string str

    Oh, and all those unchecked fopen() calls are a problem

    Add some
    if ( fp == NULL ) perror( "Cant open file" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    Dave S has a point - nobody seems to lint. Even the folks who know C here present samples (myself included) that don't test return codes, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program running long
    By smarta_982002 in forum Windows Programming
    Replies: 3
    Last Post: 03-27-2008, 05:24 AM
  2. Running my program in the background
    By shoobsie in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2005, 02:38 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM

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