Realted to File

This is a discussion on Realted to File within the C++ Programming forums, part of the General Programming Boards category; I have piece of code which HAS to use the ANSI file functions. I have a text file which stores ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    41

    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:

    Code:
    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)
    	{
    		
    		do{
    			// Read Address
    			lreadBytes = fread(lbuf,1 ,4, pFile); // where pfile is file pointer, Read 4 bytes for long
    			if(*lbuf == ' '|| *lbuf== '\t' || *lbuf == '\n')
    			{
    				continue;
    			}
    			// 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')
    				{
    					continue;
    				}
    				// Convert data from char* to double
    				lData = atof(lbuf); 
    				
    				cout<<lData<<std::endl;
    				
    				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,
    Shal

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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
    Posts
    41
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    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
    Posts
    725
    FILE* is a stream too, no?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > I want to extract these values from file and use them.
    So maybe
    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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 Cprogramming.com
    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, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21