LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2010, 05:27 AM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Rep: Reputation: 70
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.
 
Old 08-01-2010, 05:37 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Completely Clueless View Post
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 ?
 
Old 08-01-2010, 05:43 AM   #3
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
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?
 
Old 08-01-2010, 05:44 AM   #4
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
I should say I don't have access to a IDE with a nice step-through de-bugger; I've only got nano!
 
Old 08-01-2010, 05:47 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Completely Clueless View Post
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.
 
Old 08-01-2010, 05:50 AM   #6
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
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);

}
 
Old 08-01-2010, 05:53 AM   #7
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
Your compile options generated no warnings or errors.
 
Old 08-01-2010, 05:55 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Completely Clueless View Post
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'.
 
Old 08-01-2010, 06:01 AM   #9
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
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.
 
Old 08-01-2010, 06:01 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Add -O2 - this will produce the needed warning.
 
Old 08-01-2010, 06:02 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Completely Clueless View Post
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.
 
Old 08-01-2010, 06:04 AM   #12
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 08-01-2010, 06:05 AM   #13
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
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.
 
Old 08-01-2010, 06:06 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Originally Posted by Completely Clueless View Post
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
 
Old 08-01-2010, 06:21 AM   #15
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 896

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by jay73 View Post
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!

Last edited by Completely Clueless; 08-01-2010 at 07:59 AM. Reason: brain failure
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble runnin aMule nightrider Linux - Newbie 2 10-07-2007 08:20 AM
help with runnin pureftpd computerdude Linux - Networking 1 09-04-2005 04:15 AM
help with runnin pure-ftpd computerdude Linux - Software 0 09-01-2005 08:41 PM
Apache 2.0 woes ( runnin on SUSE 8.2) queendevious Linux - Newbie 2 04-01-2004 08:00 AM
runnin a counterstrike server in mandrake? weirdaliens Linux - Newbie 3 12-11-2003 08:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration