repacing printf with sprintf please help

This is a discussion on repacing printf with sprintf please help within the C Programming forums, part of the General Programming Boards category; i have to search for a word say printf in the file got as input through command line arguement whether ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    9

    Post repacing printf with sprintf please help

    i have to search for a word say printf in the file got as input through command line arguement

    whether if printf appears i have to replace it with sprintf


    please help me out with this coding

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,198
    Show us your best attempt and we will help you.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    Code:
     #include<stdio.h>
    int main(int argc,char **argv)
    {
    FILe *fp1;
    char c;
    int n;
    char a[];
    int g;
    fp1=fopen(argv[1],"wc");
    n=0;
    do
    {
    c = fgetc(fp1);
    while(c!=' ')
    {
    a[n] = c;
    n==;
    }
    printf("%s\n",a);
    g=strcmp(a,"printf");
    if(g==0)
    strcpy(a,"sprintf");
    printf("%s\n",a);
    }while(c!=NULL);
    }
    i tried with d above code
    but its not working

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Code:
    char a[];
    For one thing, you have to specify a size for your array. How many chars do you want it to hold?
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    is this the only problem?
    im getting a dump error

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    No, it definitely isn't the only problem:
    Code:
    n==;
    What the hell is that?
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    i havent got the solution yet someone solve this out for me

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Another syntax error:

    FILe *fp1;

    meant to be

    FILE *fp1;

    You must have FILE as all caps. Did you run your code through a compiler?

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    yeah i ve corrected it still its not working....

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    do anyone have an alternate code for this?

  11. #11
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    You can't really always expect the computer to understand the way you want it done. You have some logic issues in your code, primarily the fact that you are not replacing printf with sprintf in the file. You are merely just copying it to an array (which is totally unrelated to writing it back to the file).

    A perhaps not so efficient but simple way would be to copy everything in the original file exactly as it is except for the case when you have a "printf" word. In which you instead write to the temporary file "sprintf", once you finish with the file, simply copy from the temporary file to the original file. In addition, you may want to take into account of situations such as printf() being regarded as 1 word. Yet you may also need to consider another case to satisfy printf () being changed to sprintf ().

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When you "correct your code" and are still having problems, you should post your new version of the code so we all can look at it and show you what's wrong with it. It also helps if you post your warnings and/or errors that your compiler generates.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    9
    i dont really understand the logic behind this... im still thinking how to change my code

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Basicly what you have to do is read the file into memory, search through the stored stuff in memory replacing each word, and then write the whole thing back out.

    You'll probably want to use a linked list for each section of words and whitespace, or just store the thing a character at a time. In fact, that may be the easiest way for you.

    Read one character.

    If it's not the start of the word you're looking for, write it to your new file.

    Otherwise, read characters into a buffer so long as they match the search phrase.

    If you "match", meaning, all the characters you've just read in make up the search phrase, write the new phrase out to the new file, and dump the buffer you've just filled (get rid of it).
    Then continue on with step one again.

    If at any point the characters you're reading into the buffer stop matching the search phrase, then you write the contents of the buffer to the new file, and continue on with step one.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    i find if i'm having trouble is to write it as plain english first either on paper or in you .c file and enclose it in comments. Then when i do the coding i can think more clearly about what i am trying to achieve rather than diving in head first.

    it is a good habit to get into especially for larger projects where you must understand the objectives very well before you code anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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