Conditional Statements

Discussion in 'C Programming' started by Gregc., Apr 26, 2006.

  1. Gregc.

    Gregc. Guest

    Hi

    I am writing a conditional statement, using strcat. What I am trying
    to do is to add the file extenstion '.txt' if it doesn't already exist.
    What I am having trouble with is returning a value if one already
    exists. Attached is the code below:

    #include <stdio.h>
    #include <string.h>
    const int MAX_FILENAME = 256;
    oid checkFilename (char *filename, char *extension)
    {
    int length;
    int check;
    length = strlen(filename);
    check=strcmp(filename,extension);

    if(check==0)
    return;
    else
    strcat(filename,extension);

    return;
    int main () {
    char filename1 [MAX_FILENAME] = "testfile";
    char filename2 [MAX_FILENAME] = "testfile.txt";
    char filename3 [MAX_FILENAME] = "testfile.dat";
    char extension [] = ".txt";

    checkFilename (filename1, extension);
    checkFilename (filename2, extension);
    checkFilename (filename3, extension);

    printf("1: %s\n", filename1);
    printf("2: %s\n", filename2);
    printf("3: %s\n", filename3);
    }

    Could someone point me in the right direction.

    Greg
     
    Gregc., Apr 26, 2006
    #1
    1. Advertising

  2. Gregc.

    Zero Guest

    Gregc. schrieb:

    > Hi
    >
    > I am writing a conditional statement, using strcat. What I am trying
    > to do is to add the file extenstion '.txt' if it doesn't already exist.
    > What I am having trouble with is returning a value if one already
    > exists. Attached is the code below:
    >
    > #include <stdio.h>
    > #include <string.h>
    > const int MAX_FILENAME = 256;
    > oid checkFilename (char *filename, char *extension)
    > {
    > int length;
    > int check;
    > length = strlen(filename);
    > check=strcmp(filename,extension);
    >
    > if(check==0)
    > return;
    > else
    > strcat(filename,extension);
    >
    > return;
    > int main () {
    > char filename1 [MAX_FILENAME] = "testfile";
    > char filename2 [MAX_FILENAME] = "testfile.txt";
    > char filename3 [MAX_FILENAME] = "testfile.dat";
    > char extension [] = ".txt";
    >
    > checkFilename (filename1, extension);
    > checkFilename (filename2, extension);
    > checkFilename (filename3, extension);
    >
    > printf("1: %s\n", filename1);
    > printf("2: %s\n", filename2);
    > printf("3: %s\n", filename3);
    > }
    >
    > Could someone point me in the right direction.
    >
    > Greg


    One solution, which works but has to be checked clearly :)

    #include <stdio.h>
    #include <string.h>

    #define MAX_FILENAME 256

    void checkFilename (char *filename, char *extension)
    {
    int length;
    int check;
    char * pToString;

    if(!strstr(filename,extension))
    {
    pToString = strchr(filename,'.');

    if (pToString)
    {
    pToString[1] = 't';
    pToString[2] = 'x';
    pToString[3] = 't';
    pToString[4] = '\0';
    }
    else
    {
    strcat(filename,extension);
    }
    }
    }

    int main () {

    char filename1[MAX_FILENAME] = "testfile";
    char filename2[MAX_FILENAME] = "testfile.txt";
    char filename3[MAX_FILENAME] = "testfile.dat";

    char extension [] = ".txt";

    checkFilename (filename1, extension);
    checkFilename (filename2, extension);
    checkFilename (filename3, extension);


    printf("1: %s\n", filename1);
    printf("2: %s\n", filename2);
    printf("3: %s\n", filename3);

    getch();
    }
     
    Zero, Apr 26, 2006
    #2
    1. Advertising

  3. Gregc.

    CBFalconer Guest

    "Gregc." wrote:
    >
    > I am writing a conditional statement, using strcat. What I am
    > trying to do is to add the file extenstion '.txt' if it doesn't
    > already exist. What I am having trouble with is returning a
    > value if one already exists. Attached is the code below:


    Look up strrchr() function.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, Apr 26, 2006
    #3
  4. Zero wrote:
    > One solution, which works but has to be checked clearly :)
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > #define MAX_FILENAME 256
    >
    > void checkFilename (char *filename, char *extension)
    > {
    > int length;
    > int check;
    > char * pToString;
    >
    > if(!strstr(filename,extension))

    Consider the situation "BlatxtBla.ext"

    > {
    > pToString = strchr(filename,'.');

    Consider the situation "Bla.Bla.ext"

    >
    > if (pToString)
    > {
    > pToString[1] = 't';
    > pToString[2] = 'x';
    > pToString[3] = 't';
    > pToString[4] = '\0';
    > }
    > else
    > {
    > strcat(filename,extension);

    Consider the situation where the buffer is not big enough to hold the
    extenstion

    > }
    > }
    > }
    >
    > int main () {
    >
    > char filename1[MAX_FILENAME] = "testfile";
    > char filename2[MAX_FILENAME] = "testfile.txt";
    > char filename3[MAX_FILENAME] = "testfile.dat";
    >
    > char extension [] = ".txt";
    >
    > checkFilename (filename1, extension);
    > checkFilename (filename2, extension);
    > checkFilename (filename3, extension);
    >
    >
    > printf("1: %s\n", filename1);
    > printf("2: %s\n", filename2);
    > printf("3: %s\n", filename3);
    >
    > getch();

    Where's the return statement?

    > }


    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Apr 26, 2006
    #4
  5. "Abdo Haji-Ali" <> wrote in message
    news:...
    >
    > Zero wrote:
    >> One solution, which works but has to be checked clearly :)
    >>
    >> #include <stdio.h>
    >> #include <string.h>
    >>
    >> #define MAX_FILENAME 256
    >>
    >> void checkFilename (char *filename, char *extension, int maxlen)
    >> {
    >> int length;
    >> int check;
    >> char * pToString;
    >>
    >> if(!strstr(filename,extension))

    > Consider the situation "BlatxtBla.ext"
    >
    >> {
    >> pToString = strchr(filename,'.');

    > Consider the situation "Bla.Bla.ext"
    >
    >>
    >> if (pToString)
    >> {
    >> pToString[1] = 't';
    >> pToString[2] = 'x';
    >> pToString[3] = 't';
    >> pToString[4] = '\0';
    >> }
    >> else
    >> {
    >> strcat(filename,extension);

    > Consider the situation where the buffer is not big enough to hold the
    > extenstion
    >
    >> }
    >> }
    >> }
    >>
    >> int main () {
    >>
    >> char filename1[MAX_FILENAME] = "testfile";
    >> char filename2[MAX_FILENAME] = "testfile.txt";
    >> char filename3[MAX_FILENAME] = "testfile.dat";
    >>
    >> char extension [] = ".txt";
    >>
    >> checkFilename (filename1, extension);
    >> checkFilename (filename2, extension);
    >> checkFilename (filename3, extension);
    >>
    >>
    >> printf("1: %s\n", filename1);
    >> printf("2: %s\n", filename2);
    >> printf("3: %s\n", filename3);
    >>
    >> getch();

    > Where's the return statement?
    >
    >> }

    >
    > Abdo Haji-Ali
    > Programmer
    > In|Framez
    >


    checkFileName( filename, extension, MAX_FILENAME );

    int checkFilename (char *filename, char *extension, int maxlen) {
    size_t nchf, nche;
    char *p = NULL;
    if ( filename!=NULL && extension != NULL ) {
    nche = strlen( extension);
    nchf = strlen( filename );
    if ( nchf > nche ) {
    p = &filename[nchf-nche];
    }
    else {
    p = filename;
    }
    if ( strcmp( p, extension ) != 0 ) {
    if ( nchf+nche < maxlen ) {
    strcpy( p, extension );
    return 0; /* success */
    }
    }
    }
    return 1; /* failed to add extension */
    }
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Apr 26, 2006
    #5
  6. Fred Kleinschmidt wrote:

    > int checkFilename (char *filename, char *extension, int maxlen) {
    > size_t nchf, nche;
    > char *p = NULL;
    > if ( filename!=NULL && extension != NULL ) {
    > nche = strlen( extension);
    > nchf = strlen( filename );
    > if ( nchf > nche ) {
    > p = &filename[nchf-nche];
    > }
    > else {
    > p = filename;
    > }
    > if ( strcmp( p, extension ) != 0 ) {
    > if ( nchf+nche < maxlen ) {
    > strcpy( p, extension );
    > return 0; /* success */
    > }
    > }
    > }
    > return 1; /* failed to add extension */
    > }


    This function doesn't append the extention when necessary, it replaces
    the last 'nche' (length of extenstion) characters of the file name with
    the extension... Consider the situation when filename is "Blah" and the
    extenstion is ".txt" the returned file name would be just ".txt";
    which, I believe, isn't what the OP wanted.

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Apr 26, 2006
    #6
  7. "Abdo Haji-Ali" <> wrote in message
    news:...
    >
    > Fred Kleinschmidt wrote:
    >
    >> int checkFilename (char *filename, char *extension, int maxlen) {
    >> size_t nchf, nche;
    >> char *p = NULL;
    >> if ( filename!=NULL && extension != NULL ) {
    >> nche = strlen( extension);
    >> nchf = strlen( filename );
    >> if ( nchf > nche ) {
    >> p = &filename[nchf-nche];
    >> }
    >> else {
    >> p = filename;
    >> }
    >> if ( strcmp( p, extension ) != 0 ) {
    >> if ( nchf+nche < maxlen ) {
    >> strcpy( p, extension );
    >> return 0; /* success */
    >> }
    >> }
    >> }
    >> return 1; /* failed to add extension */
    >> }

    >
    > This function doesn't append the extention when necessary, it replaces
    > the last 'nche' (length of extenstion) characters of the file name with
    > the extension... Consider the situation when filename is "Blah" and the
    > extenstion is ".txt" the returned file name would be just ".txt";
    > which, I believe, isn't what the OP wanted.


    Oops - it should be
    strcpy( filename, extension );

    >
    > Abdo Haji-Ali
    > Programmer
    > In|Framez
    >

    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
     
    Fred Kleinschmidt, Apr 26, 2006
    #7
  8. CBFalconer <> writes:
    > "Gregc." wrote:
    >>
    >> I am writing a conditional statement, using strcat. What I am
    >> trying to do is to add the file extenstion '.txt' if it doesn't
    >> already exist. What I am having trouble with is returning a
    >> value if one already exists. Attached is the code below:

    >
    > Look up strrchr() function.


    I'm not sure that strrchr() is going to be useful. It searches for a
    single character; the OP wants to determine whether the name ends in a
    specified string.

    An outline of a solution:

    If the name is less than 4 characters long, it doesn't end in
    ".txt".

    Get a pointer to the fourth-to-last character of the string. Use
    strcmp() to compare the (sub)string pointed to by that pointer to
    ".txt".

    This is easily generalizable to arbitrary suffixes.

    And if you append the ".txt", make sure you've allocated enough memory
    to hold it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 26, 2006
    #8
  9. Fred Kleinschmidt wrote:
    > "Abdo Haji-Ali" <> wrote in message
    > > This function doesn't append the extention when necessary, it replaces
    > > the last 'nche' (length of extenstion) characters of the file name with
    > > the extension... Consider the situation when filename is "Blah" and the
    > > extenstion is ".txt" the returned file name would be just ".txt";
    > > which, I believe, isn't what the OP wanted.

    >
    > Oops - it should be
    > strcpy( filename, extension );

    Which is even worse, it replaces the filename with the extension... I
    think you meant:
    strcat( p, extension );

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Apr 26, 2006
    #9
  10. On Wed, 26 Apr 2006 18:04:55 GMT, in comp.lang.c , Keith Thompson
    <> wrote:

    >CBFalconer <> writes:
    >>
    >> Look up strrchr() function.

    >
    >I'm not sure that strrchr() is going to be useful. It searches for a
    >single character; the OP wants to determine whether the name ends in a
    >specified string.


    starting with a dot. He can search for dots, and check that the
    remaining part of the string a) has no more dots in it and b) is the
    right pattern.

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 27, 2006
    #10
  11. Mark McIntyre <> writes:
    > On Wed, 26 Apr 2006 18:04:55 GMT, in comp.lang.c , Keith Thompson
    > <> wrote:
    >
    >>CBFalconer <> writes:
    >>>
    >>> Look up strrchr() function.

    >>
    >>I'm not sure that strrchr() is going to be useful. It searches for a
    >>single character; the OP wants to determine whether the name ends in a
    >>specified string.

    >
    > starting with a dot. He can search for dots, and check that the
    > remaining part of the string a) has no more dots in it and b) is the
    > right pattern.


    That's useful if you're asking what extension a file name has. In
    this case, the OP wanted to know specifically whether the file name
    ends in ".txt"; in that case, there's nothing special about the '.'
    character.

    Yes, you could use the approach you suggest *in this case*, but it
    doesn't generalize well to the problem of determining whether a string
    ends in a specified substring. Consider determining whether a file
    name ends in ".tar.gz"; if the file name is "foo.tar.gz", searching
    for the last '.' misses the ".tar".

    If you're always going to be looking for a suffix consisting of a '.'
    followed by one or more additional characters that will never include
    another '.', strrchr() might be useful. But personally, I'd rather
    not hardwire that assumption into my code unless it makes things
    significantly easier.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 27, 2006
    #11
  12. Gregc. wrote:
    > Hi
    >
    > I am writing a conditional statement, using strcat. What I am trying
    > to do is to add the file extenstion '.txt' if it doesn't already exist.
    > What I am having trouble with is returning a value if one already
    > exists. Attached is the code below:
    >
    > #include <stdio.h>
    > #include <string.h>
    > const int MAX_FILENAME = 256;
    > oid checkFilename (char *filename, char *extension)
    > {
    > int length;
    > int check;
    > length = strlen(filename);
    > check=strcmp(filename,extension);
    >
    > if(check==0)
    > return;
    > else
    > strcat(filename,extension);
    >
    > return;
    > int main () {
    > char filename1 [MAX_FILENAME] = "testfile";
    > char filename2 [MAX_FILENAME] = "testfile.txt";
    > char filename3 [MAX_FILENAME] = "testfile.dat";
    > char extension [] = ".txt";
    >
    > checkFilename (filename1, extension);
    > checkFilename (filename2, extension);
    > checkFilename (filename3, extension);
    >
    > printf("1: %s\n", filename1);
    > printf("2: %s\n", filename2);
    > printf("3: %s\n", filename3);
    > }
    >
    > Could someone point me in the right direction.


    I would do

    #include <stdio.h>
    #include <string.h>

    #define FILENAME_LENGTH_MAX 256

    /* Return a pointer to the start of the file extension (including the
    dot) or a pointer to the string terminator if no extension is
    found. */

    char *extension(const char *filename)
    {
    char *result, *endp;

    endp = filename + strlen(filename);
    result = endp;
    while ((result > filename) && (*result != '.'))
    result--;
    return (result == filename)? endp: result;
    }


    /* test */

    int main(void)
    {
    char filename[FILENAME_LENGTH_MAX] = "testfile";
    char *ext = ".txt";

    if (strcmp(extension(filename), ext) != 0)
    strcat(filename, ext);
    puts(filename);
    return 0;
    }


    August
     
    August Karlstrom, Apr 27, 2006
    #12
  13. In article <>,
    Gregc. <> wrote:

    >What I am trying
    >to do is to add the file extenstion '.txt' if it doesn't already exist.


    What is the result to be if the file name *is* '.txt' ?
    --
    "It is important to remember that when it comes to law, computers
    never make copies, only human beings make copies. Computers are given
    commands, not permission. Only people can be given permission."
    -- Brad Templeton
     
    Walter Roberson, Apr 27, 2006
    #13
  14. Gregc.

    Gregc. Guest

    Walter Roberson wrote:
    > What is the result to be if the file name *is* '.txt' ?
    > --

    Then it will return testfile.txt ie no change.
     
    Gregc., Apr 27, 2006
    #14
  15. "Gregc." <> writes:
    > Walter Roberson wrote:
    >> What is the result to be if the file name *is* '.txt' ?

    >
    > Then it will return testfile.txt ie no change.


    Don't you mean it will return ".txt"?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 27, 2006
    #15
  16. Gregc.

    Gregc. Guest

    Keith Thompson wrote:
    > Don't you mean it will return ".txt"?
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.


    Yep, your correct. It's suppose to add .txt if one doesn't exist, and
    it .txt already exist then there is no change.
     
    Gregc., Apr 27, 2006
    #16
  17. In article <>,
    Gregc. <> wrote:

    >Yep, your correct. It's suppose to add .txt if one doesn't exist, and
    >it .txt already exist then there is no change.


    In the original problem statement, you phrased it in terms of
    adding the file extension '.txt' if it was not already there.

    For the file which is named just '.txt' with nothing else,
    the base file name for which '.txt' would be the extension,
    would be the empty filename. It is arguable that you have to
    have a non-empty filename in order for '.txt' to be an
    extension of that filename.

    Not that it really matters to me whether '.txt' should be
    left as is or changed to '.txt.txt'. I asked only because
    I happened to notice some unusual logic in one of the suggested
    implementations; when I traced the logic I realized it was
    testing for this case, at which point I realized you had not
    definitively indicated what should happen for it.

    --
    If you lie to the compiler, it will get its revenge. -- Henry Spencer
     
    Walter Roberson, Apr 27, 2006
    #17
  18. "Gregc." <> wrote in message
    news:...
    > Hi
    >
    > I am writing a conditional statement, using strcat. What I am trying
    > to do is to add the file extenstion '.txt' if it doesn't already exist.
    > What I am having trouble with is returning a value if one already
    > exists. Attached is the code below:
    >
    > #include <stdio.h>
    > #include <string.h>
    > const int MAX_FILENAME = 256;
    > oid checkFilename (char *filename, char *extension)
    > {
    > int length;
    > int check;
    > length = strlen(filename);
    > check=strcmp(filename,extension);
    >
    > if(check==0)
    > return;
    > else
    > strcat(filename,extension);
    >
    > return;
    > int main () {
    > char filename1 [MAX_FILENAME] = "testfile";
    > char filename2 [MAX_FILENAME] = "testfile.txt";
    > char filename3 [MAX_FILENAME] = "testfile.dat";
    > char extension [] = ".txt";
    >
    > checkFilename (filename1, extension);
    > checkFilename (filename2, extension);
    > checkFilename (filename3, extension);
    >
    > printf("1: %s\n", filename1);
    > printf("2: %s\n", filename2);
    > printf("3: %s\n", filename3);
    > }
    >
    > Could someone point me in the right direction.
    >


    Yes, I think the following is closer to what you want.


    #include <stdio.h>
    #include <string.h>

    typedef unsigned int oid;
    const int MAX_FILENAME = 256;

    oid checkFilename (char *filename, char *extension)
    {
    int check=0;

    if(strchr(filename,'.')==NULL) /* no period, so no extension */
    {
    strcat(filename,extension);
    check=1;
    }
    else /* possible extension */
    {
    if(!strcmp(strchr(filename,'.'),&extension[1]))
    { /* no matching extension */
    strcat(filename,extension);
    check=1;
    }
    }

    if(check==0)
    return(1);

    return(0);
    }

    int main(void)
    {
    char filename1[MAX_FILENAME];
    char filename2[MAX_FILENAME];
    char filename3[MAX_FILENAME];
    char extension[] = ".txt";
    unsigned int error;

    strcpy(filename1,"testfile");
    strcpy(filename2,"testfile.txt");
    strcpy(filename3,"testfile.dat");

    error=checkFilename (filename1, extension);
    printf("1:%d| %s\n",error, filename1);

    error=checkFilename (filename2, extension);
    printf("2:%d| %s\n",error, filename2);

    error=checkFilename (filename3, extension);
    printf("3:%d| %s\n",error,filename3);
    }


    Rod Pemberton
     
    Rod Pemberton, Apr 28, 2006
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,193
    mfmehdi
    Oct 20, 2006
  2. Cowboy Bob
    Replies:
    1
    Views:
    3,402
    Matt Parker
    Jan 29, 2004
  3. Harry George
    Replies:
    6
    Views:
    378
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    750
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    265
    John Crichton
    Jul 12, 2010
Loading...

Share This Page