Thread: reading text-and-numbers file word by word

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    i have a further question on another segment of my code.

    i now have a binary file that has 3 rasters arranged via BSQ. i know how many pixels each raster has, and also know that each value is floating point. what i have to do is extract all of them, and separate them into their respective band, and then work with each value individually. i was thinking of using fgets to read in each line, n then separating them into individual numbers when i hit a blank space, but i realise i cannot do that as i do not know the length of each line in the binary file.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The fgets() function doesn't care how long the line is, it will put everything into the char buffer[] array, stopping at the \n char.

    The only other stop problem would be too small a value for the middle value given to fgets():

    The end of the file also stops fgets(), but it's no trouble - there's no more data, anyway.

    Code:
    char buff[1200];              //if buff[10] we'd have trouble
    fgets(buff, sizeof(buff), fp); //where fp is the right file handle variable
    
    sscanf();                  //perhaps inside a while loop
    If you still have trouble, please post up a few examples of input lines with data, rather than describe it.

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    hi adak, thanks for the code. i have solved my problem, simply by using

    Code:
    fread(band1, sizeof(float), size, infile);
    fread(band2, sizeof(float), size, infile);
    fread(band3, sizeof(float), size, infile);
    initially i thought this was not possible, as the second call of fread would go back to the beginning of the file, giving me band1 = band2 = band3. but i was told that fread actually places a file pointer at the point where it reads till, meaning that the second fread picks up from where the first leaves off....what a useful feature!!

    does fwrite also have this feature? ie if i call a 2nd fwrite will it overwrite the first fwrite or append to the end of the 1st fwrite?

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    does fwrite also have this feature? ie if i call a 2nd fwrite will it overwrite the first fwrite or append to the end of the 1st fwrite?
    Yes, it does.

    You have a file pointer which has the current position of any read or write, within the file.

    If you close a file, and then reopen it, the current position (cp), depends on the way the file was opened:

    1) for reading or writing, cp is 1st byte of the file

    2) for append, cp is one byte beyond the (previous), last byte of the file.

    You can change the cp with fseek(), rewind(), or any kind of reading or writing to the file.
    You can also set the cp for any place in a file, as you open it, if you choose to do so.

    The above refers to "stream io" in C, which is "high level" file handling (also the normal mode, today). If you go "low level", then file handling is different, but I haven't used that mode enough to know it's in's and out's.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    is there an efficient way to read a numbers-and-text file? what i am currently doing is to use fgets() to read in a file line by line, and for each line i use strstr() to find if my desired quantity is present. if it is, i convert every character to its ascii integer, and if the character is a number or full-stop, i output it character by character to a dummy file, which i later open and read in line by line and convert each line of string to float using atof(). i find this very inefficient, and if say a line has got 10 different numbers and i want only the 4th number (not digit, but number), this gets very tedious. my code for the above would look something like this:

    Code:
    while (fgets(str, LENGTH, infile_hd) != NULL) {				
    			
    		if(strstr(str, str_samples) != NULL) {
    			len = strlen(str);
    			for (j=0; j<len; j++){
    				ch = (int)str[j];
    				if (ch > 47 && ch < 58) {
    					fprintf(outfile_hd_dummy, "%c", str[j]);
    					}
    			}
    			fprintf(outfile_hd_dummy, "\n");
    		}
    		
    		else if(strstr(str, str_lines) != NULL){
    			len = strlen(str);
    			for (j=0; j<len; j++){
    				ch = (int)str[j];
    				if (ch > 47 && ch < 58) {
    					fprintf(outfile_hd_dummy, "%c", str[j]);
    					}
    			}
    			fprintf(outfile_hd_dummy, "\n");
    		}
    	}
    		
    	fclose(infile_hd);
    	fclose(outfile_hd_dummy);
    		
    	FILE *infile_hd_dummy = fopen("/media/Data/prog/out/hd_dummy", "rb");
    	
    	float a[2];
    	j=0;
    		
    	while (fgets(str, LENGTH, infile_hd_dummy) != NULL) {
    		
    		a[j] = atof(str);
    		j++;
    	}
    		int samples = (int)a[0];
    		int lines = (int)a[1];
    is there a more efficient way to go about doing this?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you have a string, you can use sscanf + the %n directive (which tells you how many characters were read) to read one number (not digit) at a time. Something like (untested):
    Code:
    /* get the fourth number from str */
    char *strptr = str;
    for (i = 0; i < 4; ++i) {
        int offset;
        sscanf(strptr, "%f%n", &floater, &offset);
        strptr += offset;
    }

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    s there an efficient way to read a numbers-and-text file? what i am currently doing is to use fgets() to read in a file line by line, and for each line i use strstr() to find if my desired quantity is present. if it is, i convert every character to its ascii integer, and if the character is a number or full-stop, i output it character by character to a dummy file, which i later open and read in line by line and convert each line of string to float using atof(). i find this very inefficient, and if say a line has got 10 different numbers and i want only the 4th number (not digit, but number), this gets very tedious. my code for the above would look something like this:
    When you have to search through a number and text file, using fgets for the line input, and using strstr to see if a line has the quantity you're looking for, is quite efficient.

    No problem so far.

    Then, the work stages get a little off the tracks:

    1) convert to ascii integers
    2) if number or "full stop"then output char by char to a file.
    3) Then later, read in file, line by line, and convert each line of string, to float
    4) only some of these numbers are needed.

    Do you need to make all these conversions?

    Changing char's to int's, back to char's in #2, then to float in #3 for a lot of numbers that may not be needed at all, sounds like too much work.

    First rule of handling large amounts of data, is to throw out whatever you can, as quick as you can. If you don't need a bunch of numbers, don't write them into your data or file.

    If you can work with your char's, as char's, then do so, instead of converting them all to int's, back to char's, and finally to float's.

    If you can't work with the data as char's, then try and change them to floats in one step, or into a datatype that you can work with, in one step.

    Imagine that you had this same task to do by hand. How would you do it, step by step? Humans tend to be quite efficient once they work at a task for awhile. Make your program follow a similar algorithm, if you can't find anything better it can use.

    I don't need to see your code at this point, but if you could post a sample of your input, your "target values" you're searching for, and output you need, that would put more light on the matter.

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    45
    thx got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice reading lines from a text file.
    By Fujitaka in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 09:43 PM
  2. Reading numbers from a text file
    By wolfindark in forum C++ Programming
    Replies: 12
    Last Post: 03-24-2007, 01:57 PM
  3. How to copy a word or numbers from text to other location
    By trancedeejay in forum C Programming
    Replies: 12
    Last Post: 02-09-2006, 06:43 AM
  4. Read word from text file (It is an essay)
    By forfor in forum C Programming
    Replies: 7
    Last Post: 05-08-2003, 11:45 AM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM