Thread: Very frustrated and tired of asking for help

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    Very frustrated and tired of asking for help

    This board has been very helpful to me and I really appreciate it. However, I'm stuck on a project and reading the book, asking on irc, and forums is not getting me any closer to seeing what I'm doing wrong. I actually got banned from an irc chan for aksing " noob" questions. This is what I need to do:

    I need to read a text file. All the data in the file is on one line, like this:

    12.4
    wxy
    0
    text
    4
    -1

    Now I only want the actual numbers from these files. When I get anything that is not a number I need to flush out the stream and get the next line. So far in my code when I get anything but a number the program stops: For example, after I read in 12.4 I get to wxy, I need to skip (flush) wxy and get 0 skip text and get 4 and get -1.

    My problem is I just do not see how to flush out the stream. This is very frustrating especially since my lack of understanding is not because I'm not trying or being lazy or want someone to do it for me. I just don't see what I need to do here.

    Code:
      #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define FLUSH while (fgetc(sp) !='\n')
    int stats(double data[], int b,char fileName[]);
    
    
    int main(void){
      
        char fileName[100];
        double  data[100];
        FILE* sp;
        int k = 0;
        int b = 0;
        int i = 0;
       
       
      
        printf("Enter File Name:  ");
        scanf("%s",fileName);
        printf("\n\n");
        
        sp = fopen(fileName,"r");
       
        if(!sp){ /* there is no file with given filename*/
            printf("Error could not open file %s for read. \n\n",fileName);
            exit(0);
        }else if( fgetc(sp)==EOF){ /* no data in file,fileName valid */
            printf("There were 0 files in %s.\n",fileName);
            exit(0);
        }
        
     I think that I should call a
     flush here before I read  into data[]: I think that 
    I should open the buffer,check the buffer for 
    incorrect data (flush), read from buffer into data[]
     
        while(k=(fscanf(sp,"%lf",&data[i++]) == 1)) {
            b += k++ ;
            
        }
     
        fclose(sp);
        stats(data,b,fileName);
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We've been here before.
    This:
    Code:
    else if( fgetc(sp)==EOF){ /* no data in file,fileName valid */
            printf("There were 0 files in %s.\n",fileName);
            exit(0);
        }
    is still bad, wrong, and evil. Don't do it. All you're doing is throwing away the first character that you're never going to get back.

    And your comment is right -- so DO IT ALREADY.
    Code:
    while ((k=fscanf("%lf", &temp) != EOF) {
    /*again, you CANNOT check for 1, since that means you will stop on incorrect input; you MUST NOT STOP until you get to EOF*/
        if (k==0) {
            /* invalid so get rid of it */
            FLUSH;
        } else {
            /* valid input, so put it in the array */
            data[i++] = temp;
        }
    }
    Again, a very important skill to have is the ability to read what you have written -- to look at that while loop and say "hey that stops when I don't read something -- that ain't right" and fix it. If you treat C as a write-only language, then you won't get very far.

  3. #3
    Why bbebfe is not bbebfe? bbebfe's Avatar
    Join Date
    Nov 2008
    Location
    Earth
    Posts
    27
    As tabstop said, "else if( fgetc(sp)==EOF)" has moved the file position of file stream sp, you should use fseek(sp, 0, SEEK_SET) to move it back. Or using feof(sp) to test if EOF encountered to avoid these steps.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe you are going about this the wrong way.
    Best to use fgets to read a line. Convert it to a double using strtod. If it fails, then it isn't a double. Move on.
    No hassle with clearing the input buffer or crap like that.

    And you should not read strings with scanf: http://cboard.cprogramming.com/showp...37&postcount=9
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Really Elysia has the better answer but you could also do this:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    FILE *strIN;
    char line[1024];
    int ch, cc=0, sw=0;
    
    void done (char *why, int retv) {
    	puts(why);
    	exit (retv);
    }	
    
    void finishline () {
    	while ((ch=fgetc(strIN)) != '\n') if (ch == EOF) done("Done.",0);
    	cc=0; sw=0;
    	line[0]='\0';
    }	
    
    int main () {
    	if ((strIN=fopen("/tmp/test.txt", "r")) == NULL) done("Can't open file",-1);
    	while ((ch=fgetc(strIN)) != EOF) {
    		if (ch == '\n') {
    			line[cc]='\0';
    			if (strlen(line) > 0) {
    				printf("%s\n",line);
    				line[0]='\0';
    			}
    			cc=0; sw=0;
    			continue;
    		}
    		if (isdigit(ch)) line[cc]=ch;
    		else if (ch == '.') {		// avoid "...2", "1.2.3", etc.
    			sw++;			
    			if (sw > 1) finishline();
    			else line[cc]=ch;
    		}
    		else if (ch == '-') {		// avoid "12-3","-1-4", etc.
    			if (cc==0) line[0]='-';
    			else finishline();
    		}
    		cc++;
    	}
    	done ("Done.",0);
    }
    I'm not sure what you wanted to do with the numbers, this just prints them out. Your issue about "flushing the buffer" is really about what to do with it's content, since if you just leave in numbers you will end up with a buffer "12.404-1".
    This one doesn't accept "+1", etc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed