Thread: strings + file help!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    strings + file help!

    my goal is to retrieve names from a file, save it to an array and save its changes as to program terminates. also, the user cannot add a name that already exists, despite of its case. can someone please tell me what's wrong with my code? it can't compare efficiently those names from the file to the ones which the user entered.. i tried everything i could do... thanks for all the help

    here is a fragment of my code:

    Code:
    typedef struct{
        char name[maxChar];
    }namelist;
    
    int ind = 1;
    
    void addName(namelist names[], char aname[], int pos){
         int i, comp = 0, ind2 = ind;
         char temp[maxChar], temp2[maxChar];
         
            strcpy(temp,addname);
            for(i = 1; i < ind; i++){
               strcpy(temp2,names[i].name);
    		   if((strcmpi(temp, temp2))==0){
    			comp++;
    		   }
            }
         
            if(comp == 0){
               if(ind < pos || pos < 1) printf("\nWarning: Cannot add at position %d!\n",pos);
               else if(ind > pos && ind != 1){
    				while(ind2 >= pos){
                    names[ind2 + 1] = names[ind2];
                    ind2--;
                }
                strcpy(names[pos].name, aname);
                ind++;
    		   }
               else strcpy(names[ind++].name,aname);
           }
    	   else printf("\nWarning: %s is already in the list!\n",aname);
    
    }
     
    int main(){
        int ch, pos, i;
        namelist names[100];
        char name[30], c[30];
        FILE *file;
        
        file = fopen("CRUSHLIST.txt", "r+");
        
        printf("\nRetrieving CRUSHLIST.txt...\n");
        
        if(file==NULL) {
        printf("\nError: can't open file.\n");
        exit(1);
        }
        else {
        printf("\nFile opened successfully! Retrieving contents...\n");
            for(i = 1; fgets(c, sizeof(c), file)!=NULL ; i++){
               if(c[strlen(c)-1] == '\n') c[strlen(c)-1] = 0;
               strcpy(names[i].name,c);
                  ind++; //else which is previously there was a typo.. my apologies
           }
        }
        
        do{
           ch = menu();
            switch(ch){
                       case 1: printf("Enter name: ");
                               fgets(name,sizeof(name),stdin);
                               if(name[strlen(name)-1] == '\n') name[strlen(name)-1] = 0;
                               printf("Enter position: ");
                               scanf("%d",&pos);
                               addName(names,name,pos);
                               printf("\n");
                               break;
        //some code here
    Last edited by iunah; 01-19-2009 at 07:08 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, I notice you did remove the newlines

    Are you saying this is never 0?
    Code:
    		   if((strcmpi(temp, temp2))==0){
    			comp++;
    		   }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            for(i = 1; i < ind; i++){
    Should that not be for(i = 0; ... ) ?

    Code:
               if(c[strlen(c)-1] == '\n') c[strlen(c)-1] = 0;
               strcpy(names[i].name,c);
                  else ind++;
    
    What does this do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Quote Originally Posted by MK27 View Post
    Well, I notice you did remove the newlines

    Are you saying this is never 0?
    Code:
    		   if((strcmpi(temp, temp2))==0){
    			comp++;
    		   }
    i'm saying that if ever temp and temp2 is 0 (that is, if they are the same), comp's value will change and that would mean the user cannot input the name he wants because it is already existing in the list

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Quote Originally Posted by matsp View Post
    Code:
            for(i = 1; i < ind; i++){
    Should that not be for(i = 0; ... ) ?

    Code:
               if(c[strlen(c)-1] == '\n') c[strlen(c)-1] = 0;
               strcpy(names[i].name,c);
                  else ind++;
    
    What does this do?

    --
    Mats


    it was instructed that index 0 of the array will not be used..

    EDIT: my apologies, that was a typo
    else is not included..

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I would say that is so. You may just as well throw in a break when you do comp++, since it's not important whether you find one or more matches.

    Code:
            strcpy(temp,addname);
            for(i = 1; i < ind; i++){
               strcpy(temp2,names[i].name);
    		   if((strcmpi(temp, temp2))==0){
    			comp++;
    		   }
            }
    Why do you feel you need to copy the strings here?
    You should be able to just pass the original values to strcmpi() without copying the strings.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd check that. I believe fgets() will add an end-of-string char to the name, if there is enough room, but it won't take the newline from the buffer.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    I'd check that. I believe fgets() will add an end-of-string char to the name, if there is enough room, but it won't take the newline from the buffer.
    It will copy the newline from the buffer to the string, except when there is no space left for the newline (according to the given size). Either way the null terminator will be added.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Quote Originally Posted by matsp View Post
    You should be able to just pass the original values to strcmpi() without copying the strings.

    --
    Mats
    i tried passing the original values but still, nothing happened.. where do you think is the problem?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iunah View Post
    where do you think is the problem?
    That is what I was trying to ask you!

    I know what strcmp does and I can see what you intend, but when you complain "it can't compare efficiently those names from the file" I wondered how you know this. So once again, Are you saying this is never 0?
    Code:
    		   if((strcmpi(temp, temp2))==0){
    			comp++;
    		   }
    Like, in reality, if you thru in this:
    Code:
    		   if((strcmpi(temp, temp2))==0){
                            puts("A MATCH!");
    			comp++;
    		   }
    Do they ever match? I am not going to make up an input file, compile and run this program, and I bet no one else is either...if you explain yourself further...get it?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iunah View Post
    i tried passing the original values but still, nothing happened.. where do you think is the problem?
    I don't think your code was WRONG in the part that I posted - just inefficient by needlessly copying strings that didn't need copying - which is bad coding, but not WRONG.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Quote Originally Posted by MK27 View Post
    That is what I was trying to ask you!

    I know what strcmp does and I can see what you intend, but when you complain "it can't compare efficiently those names from the file" I wondered how you know this. So once again, Are you saying this is never 0?
    Code:
    		   if((strcmpi(temp, temp2))==0){
    			comp++;
    		   }
    Like, in reality, if you thru in this:
    Code:
    		   if((strcmpi(temp, temp2))==0){
                            puts("A MATCH!");
    			comp++;
    		   }
    Do they ever match? I am not going to make up an input file, compile and run this program, and I bet no one else is either...if you explain yourself further...get it?
    okay.. for example:

    FIle contains:
    John
    Matt
    Philip

    then the contents of the array would be that 3 names.. then the user has an option to add a name:

    Add a name: jOhn
    Warning! Cannot add jOhn because it is already in the list!

    to simply put it, jOhn and John is a match..

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And how is that different from what you expect?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    25
    Quote Originally Posted by matsp View Post
    I don't think your code was WRONG in the part that I posted - just inefficient by needlessly copying strings that didn't need copying - which is bad coding, but not WRONG.

    --
    Mats
    ok.. my bad.. thanks for correcting me

    but how about the problem?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think I've already asked a question as to what you expect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM