Thread: alphabet order problem(Datafile)

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    alphabet order problem(Datafile)

    I'm trying to arrange some statements in an alphabetical order
    (this is just a prototype to illustrate the problem I'm facing with my actual program)
    so the program should read the statements from a text file , arrange, then print them back on the same text file(overwrite)

    problem :
    the pointers seem to read the non-updated lines rather than reading the ones updated by the previous execution of the for loop ..
    here's the code :
    insert
    Code:
    #include<stdio.h>
    #include<string>
    
    int main()
    {
    FILE *fp1,*fp2,*fp3 ;
    char name[10],name1[10] ;
    int i,ID,ID1 ;
    
    fp1 = fopen("test.txt","r+") ;
    fp2 = fopen("test.txt","r+") ;
    fp3 = fopen("test.txt","r+") ;
    
    fseek(fp2,9,SEEK_SET) ;
    for(i=0;i<3;i++)
    {
    fscanf(fp1,"%s\t%d",&name,&ID) ;
    fscanf(fp2,"%s\t%d",&name1,&ID1) ;
    if(name[0] < name1[0])
    { fprintf(fp3,"%s\t%d\n%s\t%d\n",name,ID,name1,ID1) ;}
    if(name1[0] < name[0])
    {fprintf(fp3,"%s\t%d\n%s\t%d\n",name1,ID1,name,ID) ;}
    }
    fclose(fp1) ;
    fclose(fp2) ;
    fclose(fp3) ;
    return(0) ;
    }
    the file before the code execution:

    ccccc 3
    bbbbb 2 -------------------> (read twice)
    fffff 6
    eeeee 5
    aaaaa 1
    ddddd 4

    the file after executon:

    bbbbb 2
    ccccc 3 ----> should've been read
    bbbbb 2
    fffff 6
    eeeee 5
    fffff 6

    note : this code will be executed more than once using additional for loops to ensure full arrangement.
    any ideas are welcomed .. your help would be appreciated .

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you want to update a file, then open it "ONCE" for update.

    Multiple handles to the same file just doesn't work.
    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.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Also, run it once (not 3 times). See the output (and post it). Then run it again to see the output, so you will have a better understanding of what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  2. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  3. changing alphabet order
    By firipu in forum C Programming
    Replies: 2
    Last Post: 12-17-2007, 01:56 PM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM