Checking array for string

This is a discussion on Checking array for string within the C Programming forums, part of the General Programming Boards category; I did now, I thought the red text was just important, but they turned out to be links . Silly ...

  1. #61
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I did now, I thought the red text was just important, but they turned out to be links . Silly me.

    I now used this in stead of scanf:

    Code:
        printf("Give the name of the file you wish to analyze.\n");
        printf("-> ");
        
        fgets(datafile, 1024, stdin);
    But now if i type the string blablabla and use:

    Code:
    printf("File '%s' not found...\n", filenames[0]);
    It prints this on the screen:
    File 'blablabla
    ' not found...

    I read, if I wanted to get rid of the unwanted newline I should use:
    Code:
    buffer[ strlen(buffer) ] = '\0';
    But where do I use it? I tried it above fgets and below, and used "datafile" in stead of "buffer". I still get the unwanted newline.
    Nothing to see here, move along...

  2. #62
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Right, it's a bug actually. It should be:
    buffer[strlen(buffer) - 1] = '\0';
    Use it right after fgets to get rid of the newline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #63
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,659
    That is a bug too, in the edge case where there is no newline in the string buffer. I have suggested the use of strchr(), but I suppose just checking to see if that character is a newline would be good enough.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #64
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    The FAQ needs to be updated, but I am not too familiar with the usage of C string search functions...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #65
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    After you type the filename on screen you press enter to continue, so isn't that always a newline? It does work now bytheway.

    Could you help me with another problem too? It's still about the same program, I could make a new thread, but before you know this whole forum is stuffed with my questions...

    I'm having trouble constructing a loop that keeps asking for a filename until the filename given, exists.
    This is what I came up with, but crashes once you give the filename that exists (after giving an incorrect one):
    Code:
        printf("Give the name of the file you wish to analyze.\n");
        printf("-> ");
        
        fgets(datafile, 1024, stdin);
        datafile[strlen(datafile)-1] = '\0';        
        printf("\n");
        
        memcpy(filenames[0], datafile, 1024); //put datafile in filenames array
        FILE* file0 = fopen(filenames[0], "r");
    
    
        if (file0 == NULL)
            {              
            while (filefound == 0)
                {
                printf("File '%s' not found...\n", filenames[0]);
                printf("Give the name of the file you wish to analyze.\n");
                printf("-> ");
        
                fgets(datafile, 1024, stdin);
                datafile[strlen(datafile)-1] = '\0';
                printf("\n");
                
                memcpy(filenames[0], datafile, 512);
                FILE* file0 = fopen(filenames[0], "r");
                if (file0 == NULL)
                    {
                    filefound = 0;
                    }
                    else
                    {
                    filefound = 1;
                    }   
                }
            }
            else
            {
            lines_in_file0 = Linecount(filenames, 0);
            printf("No of lines in %s: %d \n", filenames[0], lines_in_file0);
            rewind(file0);
            
            //for (i=0;i<(lines_in_file0/3);i++)
            //    {
            //    Analyse3(file0, Startypes);
            //    printf("Lines analyzed: %d \n", (i*3));
            //    } 
            }
    Nothing to see here, move along...

  6. #66
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Quote Originally Posted by Ayreon View Post
    After you type the filename on screen you press enter to continue, so isn't that always a newline? It does work now bytheway.
    Yes, but if what you enter is longer the the buffer, fgets will read only the number of characters in your buffer and stop. That means it will not read the entire line, so the newline won't be in the buffer.

    Could you help me with another problem too? It's still about the same program, I could make a new thread, but before you know this whole forum is stuffed with my questions...

    I'm having trouble constructing a loop that keeps asking for a filename until the filename given, exists.
    This is what I came up with, but crashes once you give the filename that exists (after giving an incorrect one):
    It would help if you showed how you define datafile and filenames.
    And why do you copy over datafile to filenames anyway? If that's the case, you can read directly into filenames[0].
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #67
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    The definitions:

    Code:
        int i = 0;
        int lines_in_file0 = 0;
        int filefound = 0;
        char filenames[10][1024];
        char datafile[1024];
    Oh, and filenames is an array now, which makes no sense bacause I'm using only one filename. This is just so I can change it more easily if I want to analyze more files.

    And why do you copy over datafile to filenames anyway? If that's the case, you can read directly into filenames[0].
    I'm not sure I understand the problem. I thought I had to do it this way, because I could not assign an array in C, or something...
    Nothing to see here, move along...

  8. #68
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I understand what you meant now. I've now replaced datafile with filenames[0]. No need for datafile ofcourse!
    Now I don't need that srcpy() or memcpy() either.

    But unfortunatly that did not cause the crash .
    Nothing to see here, move along...

  9. #69
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Quote Originally Posted by Ayreon View Post
    I'm not sure I understand the problem. I thought I had to do it this way, because I could not assign an array in C, or something...
    You cannot copy arrays, but why do this:

    Define x, y
    Read into x
    Copy into y
    Use y

    When you can simply do:
    Define y
    Read into y
    Use y

    ?

    I'll look into the crash tomorrow, if no one else has.
    Last edited by Elysia; 03-07-2009 at 02:47 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #70
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Thanks, you're right, I believe I've fixed that now .
    Nothing to see here, move along...

  11. #71
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I've fixed the crash. I've been going through it with a friend of mine, he noticed that the program got stuck in the if loop, it never got to else. I've removed the last else and it works fine now.

    Also, I've changed the second FILE* file0=... to file0=... That seemed to have fixed another crash. Apparently it didn't like defining file0 twice.
    Nothing to see here, move along...

  12. #72
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    It's certainly possible to define it twice. The latter one will hide the first one, thus you can only access and change the local one inside the scope.
    The original file variable won't be affected.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #73
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    Ah, so once i got out of the if scope, the only file0 I could use, was the one with the wrong filename (The one that got me into the if scope in the first place) right?
    Nothing to see here, move along...

  14. #74
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #75
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    I've got a working program now, I only want to know if it is reliable. Where should I post this. In general people aren't happy with entire code posts, but you do need it in order to see if some things can be optimised.
    Should I just continue posting here?
    Nothing to see here, move along...

Page 5 of 6 FirstFirst 123456 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-20-2005, 11:39 PM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21