Thread: number in string

  1. #1
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110

    number in string

    so i read files with fopen
    i read a string with a certain lenght( wich is defined with strlen)
    then i say if theres a character wich is digit (isdigit(c))
    then he has to read the string until it finished presuming there are only digits after the first digit
    now my problem is i want to store the numbers in a variable how should i do
    heres some of the code...
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #define size 100
    
    int main(){
    	FILE *data;
    	char lijn[size];
    	char c;
    	int lengte;
    	int teller=0;
    	int OFM;
    
    
    	data=fopen("dat.txt","r");
    	if(data==0){
    					printf("Kon het bestand dat.txt niet openen");
    					exit(-1);
    					}
    
    	do{
    		fgets(lijn,size,data);
    		lengte=strlen(lijn);
    		for(int i=0;i<lengte;i++){
    											c=lijn[i];
    											printf("%c",c);
    											if(i==lengte){printf("\n");}
    											if(isdigit(c){
    																/*here has to be some code that reas until the string is finished and puts the number in a variable*/
    											}
    		}
    		while(!feof(data));
    
    
    	fclose(data);
    	return 0;
    	}
    thx
    Last edited by GanglyLamb; 12-10-2002 at 07:18 AM.

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    now my problem is i want to store the numbers in a variable how should i do
    a = 5;

    ---

    For real help, you need to be more specific.
    It is not the spoon that bends, it is you who bends around the spoon.

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    say my file looks like this because a=5 that was something i knew
    my file looks like this "dat.txt"
    Code:
    the first line blablabla:12356
    second line some strange words in here:1254
    now i take a string wich is one line in the file then i check every character in the string for digits and if a character is a digit then it has to read on and store for instance 12356
    so if the character in the first string becomes a nulber like here 1 then it has to read on and store the numbers left in a int....
    i hope its much clearer now my prob ...

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Code:
    while(fgets(lijn,size,data) != NULL) /* fgets returns NULL on EOF */
    {
       /* do your stuff */
    }
    You want to store all digits in one integer?
    Code:
    #include <stdio.h>
    ...
    int i;
    if(sscanf(lijn, "%d", &i) == 1) /* sscanf returns number of items successfully scanned */
       printf("%d\n", i);
    Or:
    Code:
    #include <stdlib.h>
    ...
    int i
    i = atoi(lijn); /* use the isdigit function to check if lijn starts with a digit */
    Succes,
    Monster

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    thx monster i guess uve got the right answer ill check if it works when im done with working for school......
    ive got an exam of french tommorow

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    A small example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
       char buf[BUFSIZ]; /* BUFSIZ is defined in stdio.h (1024) */
       char *ptr = NULL;
       int getal = 0;
    
       FILE *fp = NULL;
    
       if((fp = fopen("dat.txt", "r")) != NULL)
       {
          while(fgets(buf, BUFSIZ, fp) != NULL)
          {
             /* walk trough buf and stop when digit found */
             for(ptr = buf; !isdigit(*ptr); ptr++) ;
    
             if(isdigit(*ptr)) /* found a digit? */
             {
                sscanf(ptr, "%d", &getal); /* or getal = atoi(ptr); */
                printf("line  = %s", buf);
                printf("getal = %d\n", getal);
             }
          }
          fclose(fp);
       }
       return 0;
    }
    Goodluck with your exam

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>sscanf(ptr, "%d", &getal);
    If you want to use this function, make sure you check its return code to ensure it did what you asked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    ive tried to do this all but it gave me some errors so i quit thinking about it because i dont have the time right now but ill review my thread as soon as i have some spare-time that i can use to program+ i think i did use sscanf somewhere because of the example give above but i think it gave me some strange things.....
    (now i know again it was only the first digit 8 of the number 80 that was given ....somewhere) but as said ill take a look at it when i have some time ...
    Last edited by GanglyLamb; 12-10-2002 at 01:44 PM.

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Hammer
    >>sscanf(ptr, "%d", &getal);
    If you want to use this function, make sure you check its return code to ensure it did what you asked.
    You're right, in my first post I did check the scanf. Forgot that in the second post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM