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

    Join Date
    Aug 2012
    Posts
    77
    Rep Power
    0

    Post Data duplication


    i have a problem in reading in my data textfile when it have a newline that no data it duplicate the last data that it reads sample of data ..
    **********************
    1 name addres
    2
    **********************
    the output will be

    name address
    name address

    here's the some source

    void data1(FILE *fin, FILE *fout ){
    int num;
    int i = 1; //for loop
    char buff[MAX_LEN + 1];
    Amt1 = 0;

    acct[0] = NULL;
    while(!feof(fin)){
    num = fread(buff, sizeof(char ), MAX_LEN, fin );

    substring(146, 152, buff, name, sizeof name);
    substring(152, 154, buff, address, sizeof address);
    buff[num * sizeof(char )] = '\0';

    //Account data
    printf(" %s %s\n",name,address);
    fprintf(fptr1," %s %s\n",name,address,);
    }
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    You're still not getting the hang of tagging code are you?

    With [code][/code] tags, it looks like this:
    Code:
    int main ( ) {
        return 0;
    }
    Without, it looks like this:
    int main ( ) {
    return 0;
    }

    Why are you using fread() to read a text file anyway?

    > while(!feof(fin))
    read this
    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
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Ditto about choice of input function for a text file.

    Consider your loop:
    Code:
        while(!feof(fin))
        {
            num = fread(buff, sizeof(char ), MAX_LEN, fin );
    
            substring(146, 152, buff, name, sizeof name);
            substring(152, 154, buff, address, sizeof address);
            buff[num * sizeof(char )] = '\0';
    
            //Account data
            printf(" %s %s\n",name,address);
            fprintf(fptr1," %s %s\n",name,address,);	
        }
    feof() won't return true until an attempt to read from the file has failed. The way you're doing it, you make that final attempt to read past EOF, which fails. Since it failed, it never changed the buffer which still contains the last data successfully read in. But instead of testing for success and only processing the data from a successful read, you are processing the data from the previous read, the last successful one. Hence the "duplication" of the last data.

    Comments on this post

    • b49P23TIvg agrees : Also, always assume the cup you pick up is full. I didn't. Time to buy a new keyboard!
    • lhon12006 agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    77
    Rep Power
    0
    Originally Posted by salem
    You're still not getting the hang of tagging code are you?

    i used fread to Read block of data from stream

    > while(!feof(fin))
    read this
    its not working .. i read the links but nothing happens it duplicate again the data .. and i have sumthing i do inside the while .... to used the substring its that posible to interrupt it ..

    :brrr: :brrr: :confused: :confused:
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    "its not working"

    What changes did you make? Why are you expecting us to be able to read your mind?

    Did you test whether the read worked before you attempted to process the data input? As I had suggested?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    77
    Rep Power
    0
    Originally Posted by dwise1_aol
    "its not working"

    What changes did you make? Why are you expecting us to be able to read your mind?

    Did you test whether the read worked before you attempted to process the data input? As I had suggested?
    yes i made it thanks sir .... just think and analyze the problem
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Here's an idea: RTFM! ("Read the Manual") Windows/DOS compilers usually come with help files; consult them. UNIX/Linux systems usually come with man pages, invoked from a shell command line with the man command. Lacking those, you can Google on man page function-name (substituting the name of the function in question for function-name).

    Many functions have return values. You need to read the documentation for those functions to see what those return values are and to think of how you could use them. And, indeed, you at least knew enough to realize that fread returns the number of bytes read, but did you read up on what it returns when the read fails? From the fread man page at http://linux.die.net/man/3/fread:
    Return Value

    fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

    fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
    You're using each return value from fread as if it were non-zero (ie, the read succeeded). You also need to use it to see whether nothing was read in.

    Here's a stupid thought (written just today):
    Code:
        while(1)
        {
            fgets(buffer, 99, fp);
            if (feof(fp))
            {
                fclose(fp);
                break;
            }
            ParseRecord(&stars[count], buffer);
            count++;
        }
    Notice how right after I perform the read, I test feof and break out of the loop if it failed. Then only after I had eliminated the possibility that it had failed do I process that input. You could also test the return value of fread for being non-zero (success) in which case you would process the input, and then if it's zero you could close the file and exit the loop.

    The main thing is that you learn to think through what's happening.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    77
    Rep Power
    0
    how about sir in working with the path for
    example
    you have to check for the data that i read i one folder if the data is there continue to process your program else terminate ..
    and i want to use or import some com file inside my program can you give me sum example or links .. thanks a lot for your help ....
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    OK, are we hitting a human language barrier here?
    you have to check for the data that i read i one folder if the data is there continue to process your program else terminate ..
    I am a native speaker of English. I also have learned German, French, Spanish, Italian, Russian, and a half-dozen others. That line of yours doesn't make any sense at all.

    and i want to use or import some com file inside my program can you give me sum example or links
    Again, I don't know what you're trying to say there.

    Please use Standard English. No abbreviations. The abbreviations that work for your native language may be unintelligible in my own.

    I am long over-due for going to bed. If you cannot response within the next few minutes, then I'm done for the night.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    77
    Rep Power
    0
    i want to import the filename.com in my program in C language what should i do or give me some hint to read in importing some other file

    sorry for misunderstand :brrr: :brrr:
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    Originally Posted by lhon12006
    i want to import the filename.com in my program in C language what should i do or give me some hint to read in importing some other file

    sorry for misunderstand :brrr: :brrr:
    Whiskey-Tango-Foxtrot-Oscar!???

    I have absolutely not idea what you are talking about.

    Remember back to just before 1980. Before PC-DOS, the operating system that Microsoft had promised to IBM, there was Digital Research's CP/M, "Command Program/Microprocessor", which was the primary operating system for microprocessor systems. Their executable format was the .COM file, which MS-DOS continued to honor.

    So then what? You now want to somehow load and execute an ancient executable file format? Whiskey-Tango-Foxtrot-Oscar!??? (what the ****, over?).

    Just what exactly are you trying to do here?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    77
    Rep Power
    0
    sorry this is what i mean sir whats wrong with my project i cant access the path of file

    char data[7], link1[30],*link2, *link, *data1;
    char first[] = "c:\\folder\\", fname[] = "\\myfile.txt";
    path[] = first;
    int main()
    {
    printf("\n Enter First Date : ",data);
    scanf("%s",&data);
    if (!data){
    printf("No DATA Read");
    exit(0);
    }
    data1 = data;
    strcat(date, fname);
    strcat(first,date);
    printf("path\t %s",first);
    }

    it cannot read the path .. what i do??? :tntworth: :tntworth:
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,897
    Rep Power
    481
    Code:
    #include<stdio.h>		/* printf and others declared */
    #include<stdlib.h>		/* exit declaration */
    
    char data[7], link1[30],*link2, *link, *data1; 
    
    char first[] = "c:\\folder\\", fname[] = "\\myfile.txt";
    
    /*****************path[] = first;****path has no data type************/
    
    
    int main() {
      int test;  /* a new variable */
      printf("\n Enter First Date : "/*****************,data****unused by the format string************/);
      test = scanf("%s",/*****************&*******data is already a pointer*********/data);
      if (!test/*****************!data********this looks like python, not c********/){
        printf("No DATA Read");
        exit(1);
      }
      data1 = data;
      /*****************strcat(date********date is undeclared.  If you meant data instead, data has space for 7 characters and fname is longer than that*******, fname);*/
      /*****************strcat(first,date);****first is not long enough to hold more characters****************/
      printf("path\t %s",first);
      exit(0);
    }
    Last edited by b49P23TIvg; September 13th, 2012 at 07:45 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo