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
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
Show us your best attempt and we will help you.
STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.
i tried with d above codeCode:#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); }
but its not working
For one thing, you have to specify a size for your array. How many chars do you want it to hold?Code:char a[];
If you understand what you're doing, you're not learning anything.
is this the only problem?
im getting a dump error
No, it definitely isn't the only problem:
What the hell is that?Code:n==;
If you understand what you're doing, you're not learning anything.
i havent got the solution yet someone solve this out for me
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?
yeah i ve corrected it still its not working....
do anyone have an alternate code for this?
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 ().
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.
i dont really understand the logic behind this... im still thinking how to change my code
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.
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.