Thread: Realted to File

  1. #1
    Registered User
    Join Date
    May 2006

    Realted to File

    I have piece of code which HAS to use the ANSI file functions.

    I have a text file which stores 2 values: Address and Data. Its something like :

    1 233
    48 5.8

    I want to extract these values from file and use them.
    Here is how my code looks:

    int main()
    	char * lbuf = new char(8); // To store add n data
    	int lreadBytes = 0;
    	long lAddress= 0; 
    	double lData = 0;
    	FILE *pFile = fopen ("c:\\testprog1.txt","r");
    	if (pFile != NULL)
    			// Read Address
    			lreadBytes = fread(lbuf,1 ,4, pFile); // where pfile is file pointer, Read 4 bytes for long
    			if(*lbuf == ' '|| *lbuf== '\t' || *lbuf == '\n')
    			// Convert char * to long
    			lAddress= atol(lbuf);
    			cout<< lAddress;
    			// Read Data corresponding to Address
    			lreadBytes = fread(lbuf,1 ,8, pFile);// Read 8 bytes for double
    			while(lreadBytes!= 0)
    				if(*lbuf == ' '|| *lbuf== '\t' || *lbuf == '\n')
    				// Convert data from char* to double
    				lData = atof(lbuf); 
    				break; // Start reading next address		
    		}while(lreadBytes!= 0); // Keep reading till all the bytes are read.
    	return 0;
    Assume that long takes 4 bytes and double takes 8 bytes. But this code doesnt work.

    Can this code be written in more optimized manner?

    Thanks and Regards,

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    besides the cout-ing and whatnot, this seems a lot more C like than C++ like.

    >> I have piece of code which HAS to use the ANSI file functions.

    sounds like a silly limitation on a homework thing. Streams are much easier to use, so why use the FILE thing?

    >> new char(8)

    should that be new char[8];?

  3. #3
    Registered User
    Join Date
    May 2006
    Thanks for your response.
    Actually my application has to use the ANSI functions because my OS does not support streams.

    Made the correction char[8]

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    What OS is this?
    If it really didn't support streams than you wouldn't be able to use cout either.

  5. #5
    Registered User
    Join Date
    Mar 2006
    FILE* is a stream too, no?
    #include <stdio.h>
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I want to extract these values from file and use them.
    So maybe
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, pFile ) != NULL ) {
      int address;
      double data;
      if ( sscanf( buff, "%d %lf", &address, &data ) == 2 ) {
        // success
      } else {
        // a broken input line
    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. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. archive format
    By Nor in forum A Brief History of
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM