My program doesn't create a txt file when I think it should

This is a discussion on My program doesn't create a txt file when I think it should within the C Programming forums, part of the General Programming Boards category; Does anyone have any idea why this program doesn't write whether the input number is a prime or not to ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    My program doesn't create a txt file when I think it should

    Does anyone have any idea why this program doesn't write whether the input number is a prime or not to a txt file?

    Code:
    #include <values.h>
    #include <limits.h>
    #include <stdio.h>
    #include <process.h>
    #include <conio.h>
    
    int write_function();
    
    int main(void)
    {
       printf("                     Welcome to the Prime Number Program\n");
       printf("This program will determine if a number is a prime number.\n");
    
       write_function();
    
       getch();
       return 0;
    }
    
    int write_function()
    {
       FILE *prime_file;
       int number, i, remainder;
       int not_divisible = 1;
       int biggest = 1;
       prime_file = fopen("prime_io.txt", "w");
    
       printf("Enter a number(e.g. 1972): ");
       scanf("%d", &number);
       for( i = 2; i < number - 1; i++ )
       {
          remainder = number % i;
          if(remainder == 0 )
          {
             not_divisible = 0;
             biggest = i;
          }
       }
    
       if(not_divisible == 1)
          fprintf(prime_file, "\nThe number %d is a prime number", number);
       else
          fprintf(prime_file, "\nThis number's highest divisor is %d and thus is not prime", biggest);
       fclose(prime_file);
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    You could always let your program tell you why it's not. replace:
    Code:
    prime_file = fopen("prime_io.txt", "w");
    with:
    Code:
    if(!(prime_file = fopen("prime_io.txt", "w")))
    {
      perror("fopen()");
      return;
    }
    You'll also need to #include <errno.h>
    Or at the very least do:
    Code:
    if(!(prime_file = fopen("prime_io.txt", "w")))
    {
      puts("Couldn't open file for writing!");
      return;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    I get call to function write_function with no prototype, eventhough I have declared a prototype.

    The program actually does run, but when I enter in a number, then press enter, the program doesn't display whether it is prime or not, and it doesn't create a txt file.

    Any ideas?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Could anyone still help me out here? tell me if you know something, I'm an idiot if that's what it takes.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    in your prototype you need to declare your function arguments as void
    Code:
    int write_function(void);

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    It works perfectly for me. In order to get it to compile without warnings, I had to change the function prototype & definition to include void as sandman did, and include a return 0; at the end of write_function() (since it's declared to return an int). But then it created prime_io.txt very nicely and with the correct contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-02-2007, 04:40 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. how to create file in the running program
    By skyboy5122 in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:52 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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