LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Language - For Loop that just keeps on runnin' (https://www.linuxquestions.org/questions/programming-9/c-language-for-loop-that-just-keeps-on-runnin-823353/)

Completely Clueless 08-01-2010 05:27 AM

C Language - For Loop that just keeps on runnin'
 
Hi guys,

In the code segment below is a for loop I am having some considerable difficulty with. It just keeps iterating endlessly and totally ignores the 70 times limit specified. I can't ever remember having this problem before and am absolutely Clueless. Can anyone spot the error?


Code:

for ( x = 0; x < 70; x++ )

{

  fputs ("CSN00", target);
  fprintf (target, "%d", userid); 
  fputc (',', target);

  fgets (usr, 9, users);
  usr[8]=',';           
  fputs (usr, target);   
 

  fgets(pwd, 40, pass);
  fputs(pwd, target);
  userid++;

 }

BTW, 'x' is declared as type 'int' earlier in the source.

Sergei Steshenko 08-01-2010 05:37 AM

Quote:

Originally Posted by Completely Clueless (Post 4051559)
Hi guys,

In the code segment below is a for loop I am having some considerable difficulty with. It just keeps iterating endlessly and totally ignores the 70 times limit specified. I can't ever remember having this problem before and am absolutely Clueless. Can anyone spot the error?


Code:

for ( x = 0; x < 70; x++ )

{

  fputs ("CSN00", target);
  fprintf (target, "%d", userid); 
  fputc (',', target);

  fgets (usr, 9, users);
  usr[8]=',';           
  fputs (usr, target);   
 

  fgets(pwd, 40, pass);
  fputs(pwd, target);
  userid++;

 }

BTW, 'x' is declared as type 'int' earlier in the source.

How do you know the loop is running and how do you know how many times ?

Why won't print 'x' from inside the loop ?

Completely Clueless 08-01-2010 05:43 AM

Just inserted the printf x idea of yours and whilst the program runs, generating an increasingly large output file, x is stuck at only 4 for some reason. Any ideas?

Completely Clueless 08-01-2010 05:44 AM

I should say I don't have access to a IDE with a nice step-through de-bugger; I've only got nano!

Sergei Steshenko 08-01-2010 05:47 AM

Quote:

Originally Posted by Completely Clueless (Post 4051569)
Just inserted the printf x idea of yours and whilst the program runs, generating an increasingly large output file, x is stuck at only 4 for some reason. Any ideas?


Publish full source and compile with '-Wall -Wextra -Wformat=2' and make sure there are no warnings.

Completely Clueless 08-01-2010 05:50 AM

Ok, here's the complete source. In the mean time I will try your compile options...

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()

{

int x;
int userid=1;
char usr[8];    // array to store usernames
char pwd[40];  // array to store the passwords

 FILE *users;

  users=fopen("users.txt", "r");
  if(users==NULL)
    {
    printf("Error: can't open file.\n");
    return 1;
    }

  FILE *pass;

  pass=fopen("pass.txt", "r");
  if(pass==NULL)
    {
    printf("Error: can't open file.\n");
    return 1;
    }

  FILE *target;

  target=fopen("target.csv", "w+");
  if(target==NULL)
    {
    printf("Error: can't create target file.\n");
    return 1;
    }

for ( x = 0; x < 70; x++ )

{

  fputs ("CSN00", target);
  fprintf (target, "%d", userid); 
  fputc (',', target);

  fgets (usr, 9, users);  // grabs first line of user file, including newline char
  usr[8]=',';            //overwrite the newline char with a comma
  fputs (usr, target);   
 

  fgets(pwd, 40, pass);  // might need some buffer flush perhaps
  fputs(pwd, target);
  userid++;
  printf ("%d",x);

 }


fclose (users);
fclose (pass);
fclose (target);

exit(0);

}


Completely Clueless 08-01-2010 05:53 AM

Your compile options generated no warnings or errors.

Sergei Steshenko 08-01-2010 05:55 AM

Quote:

Originally Posted by Completely Clueless (Post 4051573)
Ok, here's the complete source. In the mean time I will try your compile options...

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()

{

int x;
int userid=1;
char usr[8];    // array to store usernames
char pwd[40];  // array to store the passwords

 FILE *users;

  users=fopen("users.txt", "r");
  if(users==NULL)
    {
    printf("Error: can't open file.\n");
    return 1;
    }

  FILE *pass;

  pass=fopen("pass.txt", "r");
  if(pass==NULL)
    {
    printf("Error: can't open file.\n");
    return 1;
    }

  FILE *target;

  target=fopen("target.csv", "w+");
  if(target==NULL)
    {
    printf("Error: can't create target file.\n");
    return 1;
    }

for ( x = 0; x < 70; x++ )

{

  fputs ("CSN00", target);
  fprintf (target, "%d", userid); 
  fputc (',', target);

  fgets (usr, 9, users);  // grabs first line of user file, including newline char
  usr[8]=',';             //overwrite the newline char with a comma
  fputs (usr, target);   
 

  fgets(pwd, 40, pass);  // might need some buffer flush perhaps
  fputs(pwd, target);
  userid++;
  printf ("%d",x);

 }


fclose (users);
fclose (pass);
fclose (target);

exit(0);

}


The item in red is wrong; compile should tell you so. Never compile without '-Wall -Wextra'.

Completely Clueless 08-01-2010 06:01 AM

Thanks, Sergei, but how does that cause the loop limit to be breached? And as I say, I am not getting any warnings at all with your option string.

Sergei Steshenko 08-01-2010 06:01 AM

Add -O2 - this will produce the needed warning.

Sergei Steshenko 08-01-2010 06:02 AM

Quote:

Originally Posted by Completely Clueless (Post 4051577)
Thanks, Sergei, but how does that cause the loop limit to be breached? And as I say, I am not getting any warnings at all with your option string.

I do not know whether this violation causes the problem; anyway, since your array and variables are on stack, violating array boundaries quite likely may cause broken variable value.

Completely Clueless 08-01-2010 06:04 AM

Quote:

Originally Posted by Sergei Steshenko (Post 4051580)
I do not know whether this violation causes the problem; anyway, since your array and variables are on stack, violating array boundaries quite likely may cause broken variable value.

Good point. I will look into it, thanks.

jay73 08-01-2010 06:05 AM

I get the impression you erased the \0 character terminating your string so fputs goes on printing forever because it doesn't know where to stop.

bigearsbilly 08-01-2010 06:06 AM

Quote:

Originally Posted by Completely Clueless (Post 4051571)
I should say I don't have access to a IDE with a nice step-through de-bugger; I've only got nano!

try this: ddd

it'll almost certainly be a package on your distro.

or sit down and learn how to use gdb

Completely Clueless 08-01-2010 06:21 AM

Quote:

Originally Posted by jay73 (Post 4051583)
I get the impression you erased the \0 character terminating your string so fputs goes on printing forever because it doesn't know where to stop.

Sounds distinctly possible. C# takes care of boundary-checking and the pesky NULL character automatically, I believe. Perhaps it's time I migrated!


All times are GMT -5. The time now is 04:15 AM.