Doing the impossible!!

This is a discussion on Doing the impossible!! within the C Programming forums, part of the General Programming Boards category; Hello World... I have file which contains: 2005-12-30|23:59:58|FIRST NAME|LAST NAME 2005-12-30|23:59:59|FIRST NAME|LAST NAME 2005-12-30|24:00:01|FIRST NAME|LAST NAME Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    Angry Doing the impossible!!

    Hello World...


    I have file which contains:

    2005-12-30|23:59:58|FIRST NAME|LAST NAME
    2005-12-30|23:59:59|FIRST NAME|LAST NAME
    2005-12-30|24:00:01|FIRST NAME|LAST NAME



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc,char *argv[]){
    	FILE* pFIn=0;
    	FILE* pFOut=0;
    	char pBufIn =0;
    	char puid[11]="";
    	struct tm tTime;
    	
    	int offset=0;
    	int pipecount=0;
    	int filesize=0;
    	unsigned short bits=0;
    	const int msgid[3]={11,8,9};	
    
    	//---- --------------------------------//
    	pFIn=fopen("./resend.txt","r");
    	pFOut=fopen("./resend_2.txt","a");
    
    	if(pFIn == NULL){
    		printf("File not found");
    		return 1;
    	}
    	if(pFOut == NULL ){
    		printf("File not created");
    		return 1;
    	}
    
    	fseek(pFIn,SEEK_SET,SEEK_SET);
    	
    	while(fgetc(pFIn)!=EOF){
    		fseek(pFIn,offset,SEEK_SET);
    
    		for(filesize=0;filesize<msgid[filesize];filesize++)
    		{
    			while(pipecount<msgid[filesize]){
    				pBufIn=(char)fgetc(pFIn);
    				if(pBufIn == '|')
    					pipecount=pipecount+1;
    			}
    			offset=0;
    			do
    			{
    				pBufIn=(char)fgetc(pFIn);
    				if(pBufIn=='|')pipecount=pipecount+1;	
    				else{
    					switch(msgid[filesize])
    					{
    					case 8:
    						if(bits<=sizeof(tTime.tm_year))
    						tTime.tm_year=pBufIn;
    						break;
    					case 11:
    						puid[offset]=(char)pBufIn;
    						//puid=strcat(puid,(char *)pBufIn);
    					break;
    					}
    					offset=offset+1;
    					
    				}
    			}while(pipecount < msgid[filesize]+1);
    
    			//insert a null character....
    			if(filesize==msgid[filesize])puid[offset]='\0';
    		}
    
    		fprintf(pFOut,"%s\n",puid);
    		pipecount=0;
    		do
    		pBufIn=(char)fgetc(pFIn);
    		while(pBufIn != '\n');
    		//approached the end of the line
    		offset=ftell(pFIn);
    	}
    	fclose(pFOut);
    	fclose(pFIn);
    
    	return 0;
    }

    Question: How read each byte/char and insert them into a integer.

    I want to insert the time and date accordingly based on < struct tm> type object.

    Eg: struct tm Timer;
    tm.tm_year=2005;
    tm.tm_month=12;
    and so on.

    I am currently using fgetc to get each byte.

    How do I go on about do this?


    Question No2.
    How to append a byte by byte to a pointer object:

    char A='A';
    char B='B';

    /* HOW DO I THIS?
    char* object=NULL;
    *object=(char &)A;*object++;
    *object=(char &)B;*object++;
    */

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Question: How read each byte/char and insert them into a integer.
    > while(fgetc(pFIn)!=EOF)
    Would be
    while( (ch=fgetc(pFIn)) !=EOF )

    Strictly speaking, you should only have one point where the file is read. Scattering many fgetc calls in the loop body makes for hard coding.

    Also, why don't you read a whole line using fgets?
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, pFIn ) != NULL ) {
      // parse buff for | separators
    }
    > How to append a byte by byte to a pointer object:
    Well first of all, your object needs to point somewhere other than NULL,
    then it would be
    *object++ = 'a';
    *object++ = 'b';
    *object++ = A;
    etc

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    After scanning each line, filling the struct tm object could be done like this.
    Code:
    char last[32], first[32];
    struct tm calendar;
    if ( sscanf(buff, "%d-%d-%d|%d:%d:%d|%31[^|]|%31[^\n]",
                &calendar.tm_year, &calendar.tm_mon, &calendar.tm_mday,
                &calendar.tm_hour, &calendar.tm_min, &calendar.tm_sec,
                first, last) == 8 )
    {
       time_t t;
       calendar.tm_year -= 1900;
       calendar.tm_mon  -= 1;
       t = mktime(&calendar);
       if ( t != (time_t)(-1) )
       {
          printf("%s %s %s", first, last, ctime(&t));
       }
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redefine cout to directing one file, is impossible?
    By toysoldier in forum C++ Programming
    Replies: 8
    Last Post: 08-19-2004, 11:27 AM
  2. (Im)possible Algorithm
    By Cikotic in forum C Programming
    Replies: 28
    Last Post: 03-31-2004, 07:36 AM
  3. 0% probability == impossible?
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 49
    Last Post: 09-01-2003, 03:48 PM
  4. Deleting classes, or is it impossible?
    By Lariumentiko in forum C++ Programming
    Replies: 13
    Last Post: 02-02-2003, 03:26 PM
  5. The Impossible "Surf'n'Compile" Lifestyle
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-01-2001, 11:45 AM

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