#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0

    Substring problem in data source


    //Random access to a file
    #include <stdio.h>
    #include <stdlib.h>

    char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size);
    enum {SUCCESS, FAIL, MAX_LEN = 155};
    void PtrSeek(FILE *fptr);

    void DataRead(FILE *fptr);
    int ErrorMsg(char *str);


    char CustId[10], CustLName[20], CustFName[20], CustMInit[2], DocNo[15], TranNo[7], TranType[3], Amt[10];
    int main(void){
    FILE *fptr;
    char filename[] = "c:\\file.txt";
    int reval = SUCCESS;
    //if there is some error opening file for reading…
    if((fptr = fopen(filename, "r")) == NULL){
    reval = ErrorMsg(filename);
    }
    //if opening is successful…
    else{
    printf("Emp ID No Emp Name Document No. Transaction No. Amount \n"); //8 space

    substring(92, 102, fptr, CustId ,sizeof CustId);//CustId
    substring(110, 122, fptr, CustLName, sizeof CustLName);//CustLName
    substring(131, 142, fptr, CustFName, sizeof CustFName);//CustFName
    substring(143, 144, fptr, CustMInit, sizeof CustMInit);//CustMInit
    substring(78, 92, fptr, DocNo, sizeof DocNo);//DocNo
    substring(20, 26, fptr, TranNo, sizeof TranNo);//TranNo
    substring(26, 27, fptr, TranType, sizeof TranType);//TranType
    substring(146, 154, fptr, Amt, sizeof Amt);//Amt

    if(fclose(fptr)==0)
    printf("%s successfully closed.\n", filename);
    }
    //for Borland...
    system("pause");
    return reval;
    }

    char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
    {
    int count = stop - start;
    if ( count >= --size )
    {
    count = size;
    }
    sprintf(dst, "%.*s", count, src + start);
    return dst;
    }

    //DataRead() function definition
    void DataRead(FILE *fptr)
    {
    char buff[MAX_LEN];
    //reading line of text at the fptr position…
    fgets(buff, MAX_LEN, fptr);
    //and display the text…
    printf("%s", buff);
    }
    //Error message function definition
    int ErrorMsg(char *str){
    //display this error message…
    printf("Problem, cannot open %s.\n", str);
    return FAIL;
    }

    the data in file.txt would be
    08152012031300103130010000101100000903200313SM Department Store, Inc.4031310000061123456robertojoseph B10000

    please help me ...
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481

    congratulations


    You correctly realized that passing a file pointer is not the same as passing a character pointer. This version compiles and ran once without failure on my system. I doubt it's what you intend.
    Code:
    //if your comments described the algorithm
    //"read and parse a file"
    //you'd realize that you failed to read the file.
    
    #include<stdio.h>
    #include<stdlib.h>
    
    enum{SUCCESS=EXIT_SUCCESS, FAIL=EXIT_FAILURE, MAX_LEN = 155};
    
    char*substring(size_t start, size_t stop, const char *src, char *dst, size_t size) {
      int count = stop - start;
      if ( count >= --size )
        count = size;
      sprintf(dst, "%.*s", count, src + start);
      return dst;
    }
    
    int ErrorMsg(char *str) {
      printf("Problem, cannot open %s.\n", str);
      return FAIL;
    }
    
    int main(void){
      FILE *fptr;
      int reval = SUCCESS;
      char
        *filename = "file.txt", buff[MAX_LEN],
        CustId[10], CustLName[20], CustFName[20], CustMInit[2], DocNo[15], TranNo[7], TranType[3], Amt[10];
      if (NULL == (fptr = fopen(filename, "r")))
        reval = ErrorMsg(filename);
      else {
        fgets(buff, MAX_LEN, fptr);
        if(!fclose(fptr))
          printf("%s successfully closed.\n", filename);
        puts("Emp ID No Emp Name Document No. Transaction No. Amount");
        substring(92, 102, buff, CustId ,sizeof CustId);//CustId You can't actually think this comment helps.  Don't write it!
        substring(110, 122, buff, CustLName, sizeof CustLName);
        substring(131, 142, buff, CustFName, sizeof CustFName);
        substring(143, 144, buff, CustMInit, sizeof CustMInit);
        substring(78, 92, buff, DocNo, sizeof DocNo);
        substring(20, 26, buff, TranNo, sizeof TranNo);
        substring(26, 27, buff, TranType, sizeof TranType);
        substring(146, 154, buff, Amt, sizeof Amt);
        printf("%s %s\n","CustId",CustId);
        printf("%s %s\n","CustLName",CustLName);
        printf("%s %s\n","CustFName",CustFName);
        printf("%s %s\n","CustMInit",CustMInit);
        printf("%s %s\n","DocNo",DocNo);
        printf("%s %s\n","TranNo",TranNo);
        printf("%s %s\n","TranType",TranType);
        printf("%s %s\n","Amt",Amt);
      }
      return reval;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    thanks a lot sir that's what i want to do how bout sir if i want to separate other file what will i going to do in comparing the data in substring

    AcctNo[] != (buff,AcctNo[]);
    whats wrong with this

    thanks for the help ..
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by lhon12006
    AcctNo[] != (buff,AcctNo[]);
    whats wrong with this
    So many things wrong ...

    First: the brackets. An empty set of brackets is almost always a syntax error in C. The only time I can think of they are legal is in definition of arrays with initialization (int a[] = {1, 2, 3, 4};)

    Second: the comma operator. Rule of thumb: do not use the comma operator. It hardly ever has any benefit and the code can mostly be written more legibly with other constructs. In this particular case it just serves the purpose of obfuscation.

    Third: (if it would work) the statement shown is a no-op. It does nothing at all.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    use the strcmp family of functions to compare strings.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    Originally Posted by b49P23TIvg
    use the strcmp family of functions to compare strings.
    Ok sir thanks but how could i use strcmp
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Originally Posted by lhon12006
    comparing the data in substring
    and
    Originally Posted by lhon12006
    AcctNo[] != (buff,AcctNo[]);
    gave me the vague notion that you might want to compare strings. Someday you might want to compare strings. If you learn strcmp functions then you won't have to ask why these two strings are unequal:
    Code:
    int main() {return "the"=="the";}
    
    /* exited abnormally with code 1 at Fri Aug 17 20:59:03 */
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,219
    Rep Power
    2222
    Originally Posted by lhon12006
    Ok sir thanks but how could i use strcmp
    RTFM!

    As in "Read the manual!"

    If your compiler came with documentation or a help file or man pages, then look it up!

    If not, then Google with man page strcmp. Read the man page, paying especial attention to the return values. Don't forget that strcmp is case-sensitive and has a case-insensitive version.

    There, instead of simply giving you a fish, I've taught you how to fish.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    79
    Rep Power
    0
    Im so sorry sir ..

    can you give me some example of strcmp to compare in my project

    thanks a lot sir for your help .... :D :chomp: :tntworth:

    or give me some resources to read more to better understand the strcmp... :chomp::chomp::chomp::chomp:

IMN logo majestic logo threadwatch logo seochat tools logo