Thread: read text file, convert character numbers to int?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    30

    read text file, convert character numbers to int?

    Hi!
    I wrote a small code, that reads contents of the file and puts it into multidimensional array. This is the file:
    Code:
    1:Name1:43
    2:Name2:84
    3:Name3:53
    4:Name4:14
    5:Name5:12
    6:Name6:63
    7:Name7:41
    8:Name8:31
    9:Name9:51
    10:Name10:17
    I have two problems:

    1. I need to separate each line into 3 variables, the separation character is :. for example I read the line and put values(1, Name1, 43) in 3 different variables. I need something like php explode(); function, but I guess I'll have to write it by myself.. Maybe I could use somehow fscanf to acomplish this?

    2. I have no problem reading the first character on each line and casting it as an int, so I could use it's value for adding or dividing, but when I read the last line, it's 10, that take 2 blocks in my array, how can I cast them as an integer!?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int main() {
      char c[60000];  
      int data[10][400], line_pos=0, nline=0, i=0, ch;
    	
      FILE *fd;
      fd = fopen("file", "r"); 
    
    	memset(data, 0, sizeof(data));
    
     	while((ch=getc(fd)) != EOF){
    		data[nline][line_pos] = ch;
    		line_pos++;
    
    		if(ch == 0x0a){
    			line_pos=0;
    			nline++;
    		}		
     	}
    
    	
    
        fclose(fd); 
        return 0;
    Basicly I need to read the first and the last variable from each line of the file and put them into int variable, any suggestions please? thank you!
    Last edited by hyaku_; 11-06-2006 at 03:51 PM.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    If you _want_ to use scanf, you _could_ do
    Code:
    fscanf(fileptr, "%i:%<BUF_LEN>^[:]:%i\n", &<int0>, <char *>, &<int1>);
    However, you'll need to do some error checking and watch for the EOF somewhere else.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Thanks!
    The problem is that I don't know the length of the second variable, so I won't be able to tell fscanf(); the length. Is it posible to just skip the second variable? for example:
    Code:
    fscanf(fileptr, "%i::%i\n", &<int0>, &<int1>);
    or fscanf cannot do this!? because actualy the file is a spreadsheet and it contains 13 fields, but I don't need all of them, I need just specific fields that I can use as int.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 - read each line
    fgets( buff, sizeof buff, fileptr );

    Step 2 - parse the buffer

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, fileptr ) != NULL ) {
      if ( sscanf( buff, "%d:%[^:]:%d", &num1, name, &num2 ) == 3 ) {
        // success
      } else {
        // error
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    Thank you both, that worked!:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int main() {
    
      FILE *fd;
      fd = fopen("file", "r"); 
    
    
    int num1, num2, err_n;
    char name[100], buff[6000];
    
    while (fgets( buff, sizeof buff, fd ) != NULL) {
    
    	err_n = sscanf( buff, "%d:%[^:]:%d", &num1, name, &num2 );
    	
    	if ( err_n == 3 ) {
    		printf("%d %s %d\n", num1, name, num2);
    	} else {
    		printf("ERROR: %i\n\n", err_n);
    	}
    } 	
    
        fclose(fd); 
        return 0;
      
    }
    Might be useful to someone: Analyzing Strings with sscanf

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Salem
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, fileptr ) != NULL ) {
      if ( sscanf( buff, "%d:%[^:]:%d", &num1, name, &num2 ) == 3 ) {
        // success
      } else {
        // error
      }
    }
    Wouldn't you still want to do the whole %<some num>[^:] to keep from stepping off the end of your `name`?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Probably, though making name also BUFSIZ in length, then dealing with it separately is one way of avoiding the issue.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM