By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,872 Members | 2,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,872 IT Pros & Developers. It's quick & easy.

Why does this loop not work?

P: n/a
I can't comprehend that this loop isnt working in practise as in my head it makes perfect theoretical sense

Expand|Select|Wrap|Line Numbers
  1. while(fscanf(input, "%c", &c)==1)
  2.     {
  3.         i = 0;
  4.         while(c!=';')
  5.         {
  6.             printf("%c", c);
  7.             fscanf(input, "%c", &c);
  8.             fornavn[i] = c;
  9.             i++;
  10.         }
  11.         fornavn[i] = '\0';
  12.         fscanf(input, "%c", &c);
  13.  
  14.         i = 0;
  15.  
  16.         while(c!=';')
  17.         {
  18.             printf("%c", c);
  19.             fscanf(input, "%c", &c);
  20.             efternavn[i] = c;
  21.             i++;
  22.         }
  23.         efternavn[i] = '\0';
  24.         fscanf(input, "%c", &c);
  25.         i = 0;
  26.  
  27.         while(c!='\n' && c!='\0')
  28.         {
  29.             printf("%c", c);
  30.             fscanf(input, "%c", &c);
  31.             klasse[i] = c;
  32.             i++;
  33.         }
  34.  
its supposed to dissect a line of text from a file that is split with two semicolons, however the problem ive been having is that it repeats the last letter infinitely
Oct 26 '10 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 2,398
I recommend that you scan an entire line at a time. You can then parse the characters in the line.

This will make it easier for you to handle improper inputs (for example, if there is no semicolon on the line).
Oct 27 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.