Thread: Reading Through Files

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    7

    Reading Through Files

    I cant seem to remember.. lets say i have a file that has the following data:

    Code:
    123 890 321
    321 123 678
    345 123 432
    i only want to read the first set of digits from each row and store them in a int.

    123
    321
    345

    after i read the first 3 digits, and it hits the first space... how do i tell it to move to the next line? fseek?

    any help would be great.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you could fgets
    Code:
    fgets(buffer,sizeof buffer,fp);
    sscanf(buffer, "%d %d %d", &buffer[0],&buffer[1],&buffer[2]);
    where buffer is a character array ex
    Code:
    char buffer[BUFSIZ];
    then you have all three does that help

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>sscanf(buffer, "%d %d %d", &buffer[0],&buffer[1],&buffer[2]);
    I presume you meant
    sscanf(buffer, "%d", &myint);
    or better yet use strtol().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I got the whole first row. I figured he could deal with it that way in a two dimentional array. I just showed him a one dimension and see if he wanted to use it

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by linuxdude
    I got the whole first row. I figured he could deal with it that way in a two dimentional array. I just showed him a one dimension and see if he wanted to use it
    ... but you've used the same variable to read from and write to.
    >>sscanf(buffer, "%d %d %d", &buffer[0],&buffer[1],&buffer[2]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    oh whoops sorry didn't mean to here is the way to do it
    Code:
    int info[BUFSIZ];
    int parsed_info[BUFSIZ];
    
    fgets(info,sizeof info,fp);
    sscanf(info,"%d %d %d",&parsed_info[0],&parsed_info[1],&parsed_info[2]);
    thanx hammer

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    i dont know anything about the way the previous answers work.but according to the syntax of them all u can use them only in conditions where ur matrix contains all elements that r 3 digits. but if u want to generalize it i suggest using code something like this
    Code:
    while((ch=fgetc(fp))!=EOF)//read till the end of file
    {
    	if(ch!=' ')//if ch is not equal to space
    	p[i++]=ch;//add it to an character array 
    	else
    	{
    		while(ch!='\n' && ch!=EOF)//go to the end of line
    		ch=fgetc(fp);
    		static int j=0;// so that j is 0 only the first time
    		b[j++]=atoi(p);//convert the char array to an integer
    	}
    	// and remember to keep the final val of j
    	// it is required to know the length of b
    }
    hey and one more thing i have not checked this code. wrote it just on the fly. if there r some errors just tell me. but my idea is to give u a general idea that is all. i think this thing should work with a matrix of any dimension (given the array u declare is sufficiently large). good luck
    even a fish would not have been in danger had it kept its mouth shut.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM