Thread: program that deletes same lines in a text file

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    program that deletes same lines in a text file

    Hello. I am working on a program that deletes same lines in a text file.

    I already wrote it, but it still doesn't work. Could you please help me to find the mistake?

    Thank you.

    Code:
    #include <stdio.h>
    main(){
               FILE *f1,*f2;
               char oneword[100],filename[25];
               int c;
               long int i,j;
               char *norstring[500000];  /* I count from 1 */
               long int currentLength=0;
               i=0;
               
               printf("enter filename of the first file -> ");
               scanf("%s",filename); /* read the desired filename */
               f1 = fopen(filename,"r");
    
               int t;
               
               
               do {
               i=i+1;
               c = fscanf(f1,"%s",oneword); /* got one word from the 1st file */
    
               if(i==1){
                        norstring[1]=oneword;
                        currentLength=1;
                        }
    
                        
               
               
               
               if((c==1) && (i>1)){
    
                    t=0;
                    for(j=1;j<=currentLength;j++){
                                       if(norstring[j]==oneword){
                                                                t=1;
                                                                }
                                                  }
                    
                    
                    if(t==0){
                             currentLength++;
                             norstring[currentLength]=oneword;
                             }
                             
                                   }
    
               }while (c != EOF && i<=(500000)); /* repeat until EOF */
               fclose(f1);
    
    
               f2 = fopen("file9.txt","w");
    
               for(j=1;j<=currentLength;j++){
                                             fprintf(f2,norstring[j]); /* prints it to the file */
                                             fprintf(f2,"\n");
                                             }
               fclose(f2);
               printf("\n\ndone. file9.txt created. press any key");
               getch();       
           }
    Last edited by ferronia; 03-23-2013 at 02:20 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    norstring[1]=oneword;
    ...
    norstring[currentLength]=oneword;
    Do you understand that you are always assigning the same pointer to all elements of your array?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Hi, I have no clue about pointers. I only know how to make an array and I am used to other programing languages (Pascal).. For me norstring[1] is just the first string in my array. How can I correct it?
    Last edited by ferronia; 03-23-2013 at 02:55 PM.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    505
    You're going about the program in the wrong way.

    You need to read it in a line at a time with fgets(). For now, set the buffer to 1024 bytes and assume that no line will be longer than that.

    Then think about the logic. For a first attempt, assume a fixed limit on the number of lines. Then look up malloc() and see if you can remove that fixed limit (hint, go through the file for a first pass).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program don't read all lines of a text file
    By Netcode in forum C Programming
    Replies: 5
    Last Post: 04-13-2012, 07:45 PM
  2. Reading lines of a text file
    By george7378 in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2011, 08:39 AM
  3. How would I go about counting the lines in a text file?
    By mickpc in forum Linux Programming
    Replies: 7
    Last Post: 10-07-2009, 07:21 AM
  4. Best way to read lines out of a text file
    By movl0x1 in forum C Programming
    Replies: 9
    Last Post: 05-29-2007, 12:45 PM
  5. lines of a text file
    By face_master in forum C++ Programming
    Replies: 8
    Last Post: 11-06-2001, 07:09 AM