Thread: Switching chars in file.

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    45

    Switching chars in file.

    I have to make a program that takes the file that is firs command line argument and whenever it finds a char in that file that is same as the second command line argument it switches it with char that is third command line argument

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
        FILE *cit;
        char s[100];
        int i;
    
        cit=fopen(argv[1],"r");
        if (cit==NULL)
            return 1;
    
        fgets(s,100,cit);
        int d=strlen(s);
    
        for(i=0;i<d;i++)
        {
            if(s[i]==argv[2])
                s[i]=argv[3];
        }
        s[i]=0;
        fclose(cit);
        cit=fopen(argv[1],"w");
        if(cit==NULL)return 1;
        fputs(s,cit);
        fclose(cit);
    
    
    
        return 0;
    }

    When i run it using command prompt it shows no errors or something, yet nothing changes in file, probably because string remained the same after my for loop, but i can't tell what's wrong.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No errors you say....
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:19:16: warning: comparison between pointer and integer [enabled by default]
    bar.c:20:17: warning: assignment makes integer from pointer without a cast [enabled by default]
    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
    Join Date
    Nov 2014
    Posts
    45
    Quote Originally Posted by Salem View Post
    No errors you say....
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:19:16: warning: comparison between pointer and integer [enabled by default]
    bar.c:20:17: warning: assignment makes integer from pointer without a cast [enabled by default]

    Well, i didn't considered it as error since it appears just as a warning, i saw it, but i couldn't figure out what's the problem since both s[i] and argv[2] are chars, i don't know how one of them can be integer, is that the only problem with this code?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cdummie
    i couldn't figure out what's the problem since both s[i] and argv[2] are chars, i don't know how one of them can be integer
    s is an array of 100 chars, therefore s[i] is a char (which is an integer type). argv is a pointer to a pointer, therefore -- contrary to your claim -- argv[2] is a pointer, not a char.

    Quote Originally Posted by cdummie
    is that the only problem with this code?
    Other things to consider include:
    • Check the return value of fgets.
    • Check argc before using argv[1], argv[2] and argv[3].
    • Unless you really want it there, remove the newline character from entering the input if it was stored.
    • Instead of using the magic number 100, define a named constant.
    • Unless the return value of 1 has some special significance here, return EXIT_FAILURE from <stdlib.h> instead.
    • The return type of strlen is size_t, so consider using size_t instead of int to store the return value of strlen. If you do so, then i should also be a size_t.
    • Use descriptive names: it is not clear to me why you choose "cit" and "d".
    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

  5. #5
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    Quote Originally Posted by laserlight View Post
    s is an array of 100 chars, therefore s[i] is a char (which is an integer type). argv is a pointer to a pointer, therefore -- contrary to your claim -- argv[2] is a pointer, not a char.


    Other things to consider include:
    • Check the return value of fgets.
    • Check argc before using argv[1], argv[2] and argv[3].
    • Unless you really want it there, remove the newline character from entering the input if it was stored.
    • Instead of using the magic number 100, define a named constant.
    • Unless the return value of 1 has some special significance here, return EXIT_FAILURE from <stdlib.h> instead.
    • The return type of strlen is size_t, so consider using size_t instead of int to store the return value of strlen. If you do so, then i should also be a size_t.
    • Use descriptive names: it is not clear to me why you choose "cit" and "d".

    Thanks a lot for helping me understand this, im not really familiar with pointers (especially with "double pointers"), basically i compared a value of s[i] with the memory location of command line argument (that's how i understood it), but i fixed it.
    As for the other things you pointed out, im just a beginner in programming the only thing im doing is trying to have a code that works, but i guess i should keep them in mind. And i choose "cit" and "d" because they are descriptive in my language. Thanks again.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cdummie
    And i choose "cit" and "d" because they are descriptive in my language.
    What language might that be? The single character name "d" is unlikely to be descriptive in any language, except maybe the language of mathematics (e.g., if you are implementing an algorithm from a mathematics text that uses "d" as the conventional variable name). Such single character names are generally only used for loop/array indices, and even then they might not be used if a short yet descriptive alternative is apparent.
    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

  7. #7
    Registered User
    Join Date
    Nov 2014
    Posts
    45
    Quote Originally Posted by laserlight View Post
    What language might that be? The single character name "d" is unlikely to be descriptive in any language, except maybe the language of mathematics (e.g., if you are implementing an algorithm from a mathematics text that uses "d" as the conventional variable name). Such single character names are generally only used for loop/array indices, and even then they might not be used if a short yet descriptive alternative is apparent.
    It's first letter of the word that means length, i guess i should use three letters just like i did for "cit".

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cdummie
    It's first letter of the word that means length, i guess i should use three letters just like i did for "cit".
    If the word is not too long, use the entire word: at the very least this gives people a chance to use some online translation service to find out what it means. That said, given that this is an English based message board, it is to your advantage here to use English words as names.
    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
    Nov 2014
    Posts
    45
    You are right, i will keep that in mind for the next time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switching file name
    By Herbivore in forum C Programming
    Replies: 8
    Last Post: 03-14-2014, 10:24 PM
  2. C Program Help Reading in a file of Chars until EOF
    By spunkyk014 in forum C Programming
    Replies: 1
    Last Post: 04-13-2013, 11:03 PM
  3. Reading in select chars from a file
    By B.Grills in forum C Programming
    Replies: 6
    Last Post: 09-30-2012, 03:35 PM
  4. reading in chars from file
    By AJOHNZ in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2009, 12:50 AM
  5. Win32 File Switching within a directory
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2005, 10:20 AM