While() problem.

This is a discussion on While() problem. within the C Programming forums, part of the General Programming Boards category; Hello! I need some help to spot and fix a problem I am having with a program. The program has ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    15

    While() problem.

    Hello! I need some help to spot and fix a problem I am having with a program.

    The program has to read information from a file "input.txt" and then split and store the text by ; and newline. The problem is that the input file HAS to end with a newline, or else the problems gets stuck in an infinite loop, and I can't seem to fix the problem. Please help

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char c;
    	int plads=0; int i=0;;
    	char firstname[20]="";
    	char lastname[20]="";
    	char class[20]="";
    	FILE *in,*out;
    	in=fopen("input.txt","r");
    	out=fopen("output.txt","w");
    	while((fscanf(in,"%c",&c))==1)		
    	{
    		while(c!=';')					
    		{	
    			firstname[i] = c;
    			fscanf(in,"%c",&c);
    			i++;
    		}
    		i=0;
    		//c=' ';							
    		while(c!=';')		
    				{
    			fscanf(in,"%c",&c);
    			if(c!=';')
    				{lastname[i] = c;
    			i++;}
    		}
    		i=0;
    		while(c!='\n')
    		{
    			fscanf(in,"%c",&c);
    			if(c!='\n')
    				{class[i] = c;
    			i++;}
    		}
    		i=0;
    		plads++;
    		printf("%c%c%d;%c%c%dDTU;%s;%s;%s\n",firstname[0],lastname[0],plads,firstname[0],lastname[0],plads,class,firstname,lastname);
    		fprintf(out,"%c%c%d;%c%c%dDTU;%s;%s;%s\n",firstname[0],lastname[0],plads,firstname[0],lastname[0],plads,class,firstname,lastname);
    		memset(firstname, 0, sizeof lastname);		
    		memset(lastname, 0, sizeof lastname);	
    		memset(class, 0, sizeof class);			
    	}
    	fclose(in);
    	fclose(out);
    	return 0;
    }
    Also new to these boards, so excuse me if I am using the [ code ] function incorrectly :b

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    << Welcome to the forum, Trillykins ! >>


    I suggest putting fscanf() to bed, and using fgets() instead. It will get a full row of text at a time, and allow you to then "pick"out just what you need from the char buffer that fgets() puts the text into. (Using your own logic, or something like strtok() or sscanf().

    The big advantage is that you have the full row in one string, which you can work as many different ways picking out the words, as you please or need to.

    Code:
    char buff[120] = {'\0'};  
    while((fgets(buff, sizeof(buff), filePointer)) != NULL) {
       all your code to get what you need from buff[], goes in here
    }
    You can never overflow the buff array, which also makes fgets() good for user input from the keyboard. Just change the filePointer to "stdin" (unless you re-directed the input stream).

    kudo's for using the code tags, right away!

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    Quote Originally Posted by Adak View Post
    << Welcome to the forum, Trillykins ! >>


    I suggest putting fscanf() to bed, and using fgets() instead. It will get a full row of text at a time, and allow you to then "pick"out just what you need from the char buffer that fgets() puts the text into. (Using your own logic, or something like strtok() or sscanf().

    The big advantage is that you have the full row in one string, which you can work as many different ways picking out the words, as you please or need to.

    Code:
    char buff[120] = {'\0'};  
    while((fgets(buff, sizeof(buff), filePointer)) != NULL) {
       all your code to get what you need from buff[], goes in here
    }
    You can never overflow the buff array, which also makes fgets() good for user input from the keyboard. Just change the filePointer to "stdin" (unless you re-directed the input stream).

    kudo's for using the code tags, right away!
    Thanks for the advice but I am not sure how to use the strtok function (hence the awfully complicated while and if loops :b ), could you perhaps show me an example of how to use it to store the info up to the first ; into one array, then store the next text until the other ; and store that into another array and then the last piece of text until either a newline or the end of the file? Would be an enormous help Thank you in advance!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    Nit: The memset function requires the <string.h> header according to the standard (I believe).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I just posted a good example of using strtok() -- wait one and I'll add it here.

    This is the one, it may be a bit "clunky", but the text file was full of one word lines of text, and also longer lines of text, mixed up.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define SIZE 120
    
    int main ()
    {
    
      FILE* glo;
      char str[SIZE][SIZE] = {{'\0'}};
      //    char* str[1000];
      char* filename = "glossary.txt";
      char test[SIZE];
      char *ptr;
      int i, len;
      int r=0;
      printf("\n\n");
    
      if (!(glo = fopen(filename,"r"))) { 
        printf("\nCannot open %s for output.\n",filename);
        return 1;
      }
      ptr = NULL;
      len = r = 0;
      while(fgets(test,SIZE,glo) != NULL) {
        ptr=strtok(test, " ");
        if(ptr) {
          strcpy(str[r], ptr);
          len=strlen(str[r]);
          if(str[r][len-1]=='\n')
            str[r][len-1]='\0';
          ++r;
        } 
        while(ptr) {
          ptr = strtok(NULL, " ");
          if(ptr) {
            strcpy(str[r], ptr);
            len=strlen(str[r]);
            if(str[r][len-1]=='\n')
              str[r][len-1]='\0';
            ++r;
          }
        }
      }
      i=0;
      while(i<r) {
        if(i % 20 == 0 && i) { 
          puts("\n\n        press enter to display the next set of words");
          (void) getchar();
        }
        printf("\n %d: %s ", i, str[i++]);
      }
    
      printf("\nEnter test word [class]: ");
      scanf("%1023s",test);
      printf("\nstr[2]:%s",str[1]);
      printf("\nyours:%s",test);
    
      if(strcmp(test,str[1])==0) {
        printf("\nyes");
      }
      else {
        printf("\nWrong command. \n");
      }
    
      fclose(glo);
      return 0;
    }
    Burst out words from "glossary.txt"
    abstract
    class
    a
    class
    defined
    to
    make
    creating
    subclass
    easier
    array
    an
    ordered
    collection
    of
    values
    collection
    class
    that
    used
    for
    grouping
    and
    manipulating
    related
    objects
    compile
    time
    the
    time
    during
    which
    the
    source
    code
    is
    analyzed
    and
    converted
    into
    object
    code
    dictionary
    a
    collection
    of
    key/value
    pairs
    framework
    a
    collection
    of
    classes,
    functions
    and
    protocols
    that
    are
    related
    to
    support
    certain
    platform
    instance
    a
    concrete
    representation
    of
    a
    class
    message
    the
    method
    and
    its
    associated
    arguments
    that
    are
    sent
    to
    an
    object
    retain
    count
    a
    count
    of
    the
    number
    of
    times
    an
    object
    is
    referenced
    selector
    the
    name
    used
    to
    select
    the
    method
    to
    execute
    for
    an
    object
    */

    This was the original format of the above words:
    abstract class
    a class defined to make creating subclass easier
    array
    an ordered collection of values
    collection
    class that used for grouping and manipulating related objects
    compile time
    the time during which the source code is analyzed and converted into object code
    dictionary
    a collection of key/value pairs
    framework
    a collection of classes, functions and protocols that are related to support certain platform
    instance
    a concrete representation of a class
    message
    the method and its associated arguments that are sent to an object
    retain count
    a count of the number of times an object is referenced
    selector
    the name used to select the method to execute for an object


    The problem with this program is it doesn't do what the OP wanted - the strtok() was fine (that's why I'm re-posting it), but it doesn't make a logical link between certain terms, and their meaning. Especially difficult are the multi-word terms like "abstract class".

    If you still have problems with your while loop, post back, and include the input file, so we can see the details of what the program needs.
    Last edited by Adak; 10-02-2010 at 01:40 AM.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    The input looks like this:

    Hanne;Pinse;2010
    Dulle;Dalle;2010


    The problem is that my current program demands that the input file has to end in a newline, else it gets stuck. The fgets() seems better, but I am not sure show to extract text from the buff array, seperate them by ';' and newline/NULL, and store them in three seperate arrays (Hanne into firstname array, Pinse into lastname array, 2010 into class array). The strtok seems like a good idea, but I am just not sure how to apply it.
    Last edited by trillykins; 10-02-2010 at 07:09 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your problem is simpler - just use fgets(), (and the newline at the end of the file is not needed).

    To separate the words, use sscanf() on the buffer that fgets() is putting the text into.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i, j;  
      char buff[80]= {'\0'};
      char lname[10][30]={'\0'};
      char fname[10][30]={'\0'};
      char data[10][12]={'\0'};
      FILE *fp;
      printf("\n\n");
      if((fp=fopen("input1.txt", "r")) == NULL) {
        printf("\nError opening input1.txt");
        return 1;
      }
    
      i=0;
      while(fgets(buff, sizeof(buff), fp)) {
        j=0;
        while(buff[j]!='\n' && j < sizeof(buff)) { //replace all ';' with a space
          if(buff[j]==';')
            buff[j]=' ';
          ++j;
        }                                                 //which lets sscanf() work properly
        sscanf(buff, "%s %s %s", &lname[i], &fname[i], &data[i]);
        ++i;
      }
      fclose(fp);
      for(i=0;i<5;i++)
        printf("\n %s %s %s", lname[i], fname[i], data[i]);
    
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    Last edited by Adak; 10-02-2010 at 03:18 AM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    Damn! Okay, you are right, fgets() is so much easier, simpler to use than fscanf! The whole program now works perfectly. Thank you very much for your help!

    Quote Originally Posted by Adak View Post
    Your problem is simpler - just use fgets(), (and the newline at the end of the file is not needed).

    To separate the words, use sscanf() on the buffer that fgets() is putting the text into.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
      int i, j;  
      char buff[80]= {'\0'};
      char lname[10][30]={'\0'};
      char fname[10][30]={'\0'};
      char data[10][12]={'\0'};
      FILE *fp;
      printf("\n\n");
      if((fp=fopen("input1.txt", "r")) == NULL) {
        printf("\nError opening input1.txt");
        return 1;
      }
    
      i=0;
      while(fgets(buff, sizeof(buff), fp)) {
        j=0;
        while(buff[j]!='\n' && j < sizeof(buff)) { //replace all ';' with a space
          if(buff[j]==';')
            buff[j]=' ';
          ++j;
        }                                                 //which lets sscanf() work properly
        sscanf(buff, "%s %s %s", &lname[i], &fname[i], &data[i]);
        ++i;
      }
      fclose(fp);
      for(i=0;i<5;i++)
        printf("\n %s %s %s", lname[i], fname[i], data[i]);
    
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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