GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Oct-2004, 01:56
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Question

NOT sure what is wrong with script fscanf


Hi there,

I have been working on this script to learn C and I am hung up on some minor problem. The script is suppose to read imput.txt from my c drive then check to see if the numbers in imput.txt are Prime or not as well as perfect or not. I compiled my program and got 0 errors but when I get to the black popup box a windows pop up comes up stating these issues "Found the comment. When I execute the script it gives me no errors until it brings up the black box and then another window pops up and says Windows debug failed. Line 54 fscanf.c
Then says that Expression: Stream != NULL"

Here is my script!!! ANT CLUES? I think I have been staring at it too much. I am new to C.

CPP / C++ / C Code:
# include <stdio.h>
# include <math.h>

#define SENTINEL -999

int main ()
{
	 FILE *inp;
	 FILE *outp;
     int num, total, i, number, prime;
     float ans;
	 int IsPrim;


//Initialize
     number = 0;
	 prime = 0;
	 num = 0;

	 inp=fopen("c:input.txt", "r");
	 outp=fopen("c:output.txt", "w");

	 fscanf(inp, "%d", &num);
	 fprintf(outp, "%d", num);

     do
          {

          //Initialize
     total = 0;
     ans = 0;

// For prime
    ans = 1;
    IsPrim = 1;

    for(i = 2; i < num; i++)
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");

// For perfect

         total = 0;
         for ( i = 1; i < num; i++)
             if(num%i==0)
                total+=i;
         if(total==num)
             printf("Perfect");   
             for ( i = 1; i < num; i++)
                if(num%i==0)
                   printf("Perfect divisor");

          }
		 while(num != SENTINEL);
		  
fclose(inp);
fclose(outp);

return(0);
		  
}
Last edited by LuciWiz : 29-Oct-2004 at 04:52. Reason: Please insert your c code between [c] & [/c] code
  #2  
Old 29-Oct-2004, 07:12
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello jenmaz. Welcome to GIDForums™.

Without going over the entire code, I can see several things that jump out at me immediately.

First fopen should always be checked for errors. It will return null upon error. I suspect that your program is not opening the file properly and then choking when it is trying to use the FILE*

CPP / C++ / C Code:
  if( (inp=fopen("c:input.txt", "r") ) != NULL){
    if( (outp=fopen("c:output.txt", "w")) != NULL){
       //do lots of fun and intersting stuff..
       //...
       fclose(outp);
    }
    else
       printf("Error opening output file!\n");
    fclose(inp);
  }
  else
    printf("Error opening input file!\n");

Also, you may want to call out your text file in an absolute manner.
CPP / C++ / C Code:
if( (inp=fopen("c:\\directory\\input.txt", "r") ) 
Finally, you are only looking at one number with your code right now. Your fscanf function is outside of your do{ }while loop. Therefore, your do-while loop will loop forever because num never changes.
CPP / C++ / C Code:

	 fscanf(inp, "%d", &num);
	 fprintf(outp, "%d", num);

     do
          {

Hope this helps. Good luck!
  #3  
Old 29-Oct-2004, 08:32
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

Still cannot figure out why my input file won't open


Hi there,checked for errors and even moved my file to the D drive and it is still not being recognized. I still cannot figure out how to read from an input file. Should I try openning another file?

Here is what I have now.
CPP / C++ / C Code:
# include <stdio.h>
# include <math.h>



#define SENTINEL -999

/* Function Prototype */

/* int Prime(int n);
int Perfect(int n);*/

int main ()
{
      FILE *inp;
      FILE *outp;
     int num, total, i, prime;
     float ans;
      int IsPrim;


if( (inp=fopen("d:input.txt", "r") ) != NULL){
    if( (outp=fopen("d:output.txt", "w")) != NULL){
       //do lots of fun and intersting stuff..
       //...
       fclose(outp);
    }
    else
       printf("Error opening output file!\n");
    fclose(inp);
  }
  else
    printf("Error opening input file!\n");


//Initialize
     num = 0;
      prime = 0;

      inp=fopen("d:input.txt", "r");
      outp=fopen("output.txt", "w");


     do
          {

    /*  fscanf(inp, "%d", &num);
     fprintf(outp, "%d", num);


	Prime(int n);
	Perfect(int n); */

	 }
	while(num != SENTINEL);

fclose(inp);
fclose(outp);

return (0);
		  }
// For prime

/* int Prime(int n)
{
	int num, total, n;
 //Initialize
    total = 0;
    ans = 0;
    ans = 1;
    IsPrim = 1;

    for(n = 2; n < num; n++);
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");
	         }
return (0);
		  

}

// For perfect

	int Perfect(int n)
	{
		   int num, total, n;
 //Initialize
     total = 0;
     ans = 0;

         total = 0;
         for ( n = 1; n < num; n++)
             if(num%n==0)
                total+=n;
         if(total==num)
         {
             printf("Perfect");   
             for ( n = 1; n < num; n++)
                if(num%n==0)
                   printf("Perfect divisor");
         }
return (0);
}*/




Quote:
Originally Posted by dsmith
Hello jenmaz. Welcome to GIDForums™.

Without going over the entire code, I can see several things that jump out at me immediately.

First fopen should always be checked for errors. It will return null upon error. I suspect that your program is not opening the file properly and then choking when it is trying to use the FILE*

CPP / C++ / C Code:
  if( (inp=fopen("c:input.txt", "r") ) != NULL){
    if( (outp=fopen("c:output.txt", "w")) != NULL){
       //do lots of fun and intersting stuff..
       //...
       fclose(outp);
    }
    else
       printf("Error opening output file!\n");
    fclose(inp);
  }
  else
    printf("Error opening input file!\n");

Also, you may want to call out your text file in an absolute manner.
CPP / C++ / C Code:
if( (inp=fopen("c:\\directory\\input.txt", "r") ) 
Finally, you are only looking at one number with your code right now. Your fscanf function is outside of your do{ }while loop. Therefore, your do-while loop will loop forever because num never changes.
CPP / C++ / C Code:

	 fscanf(inp, "%d", &num);
	 fprintf(outp, "%d", num);

     do
          {

Hope this helps. Good luck!
Last edited by dsmith : 29-Oct-2004 at 09:05. Reason: Please use [c] & [/c] for syntax highlighting
  #4  
Old 29-Oct-2004, 09:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
Hi there,checked for errors and even moved my file to the D drive and it is still not being recognized. I still cannot figure out how to read from an input file. Should I try openning another file?

Here is what I have now.

Try this to see where it is looking for the file. (Change inname to whatever you think it should be.)

The program will look for the file name that you give it in all of the directories in the path.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  FILE *inp;
  FILE *outp;


  char *inname = "input.txt";
  char *outname = "output.txt";

  printf("Here are the contents of the current directory:\n");
  system("dir"); /* if this is linux, use system("ls -l"); */
  printf("\nHere is the path:\n");
  system("PATH"); /* if this is linux, use system("echo $PATH"); */
  printf("\n\n");

  if( (inp=fopen(inname, "r") ) == NULL){
    printf("Error opening input file <%s>\n", inname);
    return 0;
  }

  printf("Opened  input file <%s>\n", inname);

  if( (outp=fopen(outname, "w")) == NULL){
    printf("Error opening output file!\n");
    fclose(inp); /* not strictly necessary, since will be closed upon return */
    return 0;
  }

  printf("Opened output file <%s>\n", outname);

  fclose(inp);
  fclose(outp);

  return 0;

}



If you want to give it a directory name, then use something like
CPP / C++ / C Code:

char *inname = "c:input.txt";
system("dir c:");

or

CPP / C++ / C Code:
  char *inname = "c:\\myprogs\\input.txt";
  system("dir c:\\myprogs");

This should show you what's in the directory that you are using.

Regards,

Dave
  #5  
Old 29-Oct-2004, 11:35
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

Thanks it work but have another question with script


OK I have figured that out. The file was named input.txt.txt and I could not see that. Now I am trying to activat my functions so that when the data is read from the file it gets executed by both Prime and perfect functions to determine and print on screen if the number is prime, not prime and/or perfect, not perfect. When looking at my code do you see something on when I try to call the functions? I have 10 errors and not sure how to resolve it.

Here is the Code.

CPP / C++ / C Code:
# include <stdio.h>
# include <math.h>



#define SENTINEL -999

/* Function Prototype */

/* int Prime(int n);
int Perfect(int n);*/

int main ()
{
      FILE *inp;
      FILE *outp;
     int num, total, i, prime;
     float ans;
      int IsPrim;


//Initialize
     num = 0;
      prime = 0;

      inp=fopen("input.txt", "r");
      outp=fopen("output.txt", "w");


     do
          {

    fscanf(inp, "%d", &num);
    fprintf(outp, "%d", num);


Prime(int n);
Perfect(int n);

	 }
	while(num != SENTINEL);

fclose(inp);
fclose(outp);

return (0);
		  }
// For prime

int Prime(int n)
{
	int num, total, n;
 //Initialize
    total = 0;
    ans = 0;
    ans = 1;
    IsPrim = 1;

    for(n = 2; n < num; n++);
       if ( num % i == 0)
       {
          IsPrim=0;
          printf("Divisor of prime");
       }
    if(IsPrim)
       printf("It is Prim");
	         }
return n;
		  

}

// For perfect

	int Perfect(int n)
	{
		   int num, total, n;
 //Initialize
     total = 0;
     ans = 0;

         total = 0;
         for ( n = 1; n < num; n++)
             if(num%n==0)
                total+=n;
         if(total==num)
         {
             printf("Perfect");   
             for ( n = 1; n < num; n++)
                if(num%n==0)
                   printf("Perfect divisor");
         }
return n;
}
Last edited by JdS : 07-Nov-2004 at 06:01. Reason: Please insert your example C/C++ codes between [c] and [/c] tags
  #6  
Old 29-Oct-2004, 11:46
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
I would also like to write a function that allows the user to close or quit out of the program by typing in y for yes or n for no. The function would return a character indicating the user response for the question “Do you want to try another file?”

Any ideas. I am just now learning functions and if I see it I can understand it.

Thanks in advance,

Jennifer
  #7  
Old 29-Oct-2004, 12:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
I would also like to write a function that allows the user to close or quit out of the program by typing in y for yes or n for no. The function would return a character indicating the user response for the question “Do you want to try another file?”

Any ideas. I am just now learning functions and if I see it I can understand it.

Thanks in advance,

Jennifer

Before trying to enhance the program, I respectfully suggest that you get it working first.

You really should try to use consistent indenting to see where your control structures are.

Here is a slightly modified listing of your code. NOte that an extra "}" near the end of Perfect() completely screwed up everything afterwards.

CPP / C++ / C Code:
# include <stdio.h>
# include <math.h>



#define SENTINEL -999

/* Function Prototype */

int Prime(int n);
int Perfect(int n);

int main ()
{
  FILE *inp;
  FILE *outp;
  int num, total, i, prime;
  float ans;
  int IsPrim;


/*Initialize */
  num = 0;
  prime = 0;

  inp=fopen("input.txt", "r");
  outp=fopen("output.txt", "w");


  do {
    fscanf(inp, "%d", &num);
    fprintf(outp, "%d", num);
    Prime(int n);
    Perfect(int n);
  } while(num != SENTINEL);

  fclose(inp);
  fclose(outp);

  return (0);
}
/* For prime */

int Prime(int n)
{
  int num, total, n;
 /*Initialize */
  total = 0;
  ans = 0;
  ans = 1;
  IsPrim = 1;

  for(n = 2; n < num; n++);
     if ( num % i == 0)
     {
        IsPrim=0;
        printf("Divisor of prime");
     }
  if(IsPrim)
     printf("It is Prim");
/* } */ /*this superfluous bracket confused everything after */
  return n;
}

/* For perfect */

int Perfect(int n)
{
  int num, total, n;
  /*Initialize */
  total = 0;
  ans = 0;

  total = 0;
  for (n = 1; n < num; n++)
    if(num % n==0)
      total += n;

  if(total == num) {
    printf("Perfect");   
    for ( n = 1; n < num; n++)
      if(num % n == 0)
        printf("Perfect divisor");
  }

  return n;
}


Note that after removing this bracket there are still some errors. I'll help with a couple; you should try to see what the others are:

when calling a function, just put in the argument, not the type.

That is, change this
CPP / C++ / C Code:
  do {
    fscanf(inp, "%d", &num);
    fprintf(outp, "%d", num);
    Prime(int n);
    Perfect(int n);
  } while(num != SENTINEL);

to this
CPP / C++ / C Code:
  do {
    fscanf(inp, "%d", &num);
    fprintf(outp, "%d", num);
    Prime(n);
    Perfect(n);
  } while(num != SENTINEL);

Regards,

Dave
  #8  
Old 29-Oct-2004, 12:33
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

Thanks it is running but now I just need...


Everything is running and the script checks the data and I get output from the data. Unfortunately the Prime scripts and Perfect formulas are not correct. Since I am not good in math do you know where I could find these functions?

Jennifer

PS Thanks... I found a lot of the issues once you pointed out some
  #9  
Old 29-Oct-2004, 12:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,703
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by jenmaz
Everything is running and the script checks the data and I get output from the data. Unfortunately the Prime scripts and Perfect formulas are not correct. Since I am not good in math do you know where I could find these functions?

Jennifer

PS Thanks... I found a lot of the issues once you pointed out some


I perceive that you have the right idea for an implementation of Prime(). That is, you simply see if anything greater than 1 and less than n is an exact divisor of n

Look at your function:

CPP / C++ / C Code:
int Prime(int n)
{
  int num, total, n;
 /*Initialize */
  total = 0;
  ans = 0;
  ans = 1;
  IsPrim = 1;

  for(n = 2; n < num; n++);
     if ( num % i == 0)
     {
        IsPrim=0;
        printf("Divisor of prime");
     }
  if(IsPrim)
     printf("It is Prim");

  return n;
}

Now, as you have written it, n is the number that you passed to the function to test to see if it is prime.

Now, you can't declare a local variable with the same name as the argument that you used. so there are some obvious errors here. I would like to see your code, but to save time, I will make a slight modification:

CPP / C++ / C Code:
int Prime(int num)
{
  int  n, IsPrim;
 
  IsPrim = 1;

  for(n = 2; n < num; n++);
     if ( num % i == 0)
     {
        IsPrim=0;
        printf("Divisor of prime");
     }
  if(IsPrim)
     printf("It is Prim");
/* } */ /*this superfluous bracket confused everything after */
  return n;
}

Where I have deleted superfluous variables not used in this function. Now, what's wrong?

A typographical error: what's i in the expression
CPP / C++ / C Code:
if (num % i == 0)

Well, there is no i anywhere, so you must have meant n.

So now we have
CPP / C++ / C Code:
int Prime(int nnum)
{
  int n, IsPrim;

  IsPrim = 1;

  for(n = 2; n < num; n++);
     if ( num % n == 0)
     {
        IsPrim=0;
        printf("Divisor of prime");
     }
  if(IsPrim)
     printf("It is Prim");

  return n;
}


What's the loop doing? If it ever finds a divisor, then it knows the number is not a prime, so it sets the variable IsPrim to 0.

I would make the printout a little more informative:

CPP / C++ / C Code:
    if ( num % n == 0)
     {
        IsPrim=0;
        printf("%d is a divisor of %d\n", n, num);
     }

With this to get you started, you should be able to get a little farther. You can clean up the output to your taste. If you have any more questions, show us what you did, what you got, what you expected, and what you don't understand.

Regards,

Dave
  #10  
Old 29-Oct-2004, 13:17
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

getting what the issue is but having trouble resolving it


Hi there,

I fixed the i variable it was suppose to be n (to my knowledge). I just forgot to change it. I am still weak at looking over these minor mistakes.

I then placed your code

if ( num % n == 0)
{
IsPrim=0;
printf("%d is a divisor of %d\n", n, num);

In the script so that we could see the output.

So when I ran the program and it read these numbers from my input.txt file
6 7 28 18 496 31 24 17 36 3 19 -999

It gave me this out put which shows me that the script is reading the numbers (12 all together and stopping at -999) however it is not out puting properly because n is now = 2 not = to the input number. Then it displays n = 2 and its division and it is the same output for each 12 data imput from the text file.

Here is the output
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
2 is a divisor of -858993460
-858993460 is a divisor of 2
Press any key to continue

I understand now what is going wrong but not sure how to fix it.

Thanks in advance.
jen
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two virtual hosts, cgi script behaves differently on each blimbo Apache Web Server Forum 0 04-Aug-2004 09:35
Where can I find web statistics script / software? rhino1616 Web Design Forum 2 02-Jan-2004 20:31
Redirect multiple URLs to one script, masking true URL Maccaday Apache Web Server Forum 1 10-Dec-2003 08:34
JavaScript Tutorial Part 1 pcxgamer Web Design Forum 2 01-Dec-2003 09:16
VALIDATING file types on an PHP upload script? JdS MySQL / PHP Forum 0 02-Jan-2003 07:58

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.