1

printfすべてのステートメントを文字列値に置き換えようとしています。最初に、以下のようにすべての行を文字列に読み込んでいます。

 ifstream ifs;
 ifs.open(filename);
 string temp;
 string text;
 while(!ifs.eof())
 {
     getline(ifs, temp, '\t');
     text.append(temp);
     temp.clear();
 }

次に、すべての行を見つけてprintf、それが見つかった場合は、"printf statement". を置き換えるための私のコードprintf:

char ch;
while(getline(is,check))
{
 ch=check[0];
    if(!isalpha(ch))
     {
      //statements..
     }
    else
     {
        string str2("printf");
        size_t found;
        found=check.find(str2);
           if(found!=string::npos)
              check="\n printf statement.\n";
       OriginalStr.append(check);

         check.clear();
     }

以下のような 3 つの 4 行のファイルに対して機能します。

main()
{
Hi i am Adityaram.
and i am good boy.
and you?
printf("");
{
printf("");
Aditya
printf("");
Rammm
printf("");
Kumar
printf("");
{
printf("");

printf("");
}
printf("");
}
printf("");

これらのファイル行に printf 行が見つかりません。

main()
{
   char ch, file_name[25],*p;
   char answer[400];
   int size=0;
   FILE *fp;

   printf("Enter the name of file you wish to see ");
   gets(file_name);
}

printf行が見つからないのはなぜですか? またはどのように行うのですか?任意の提案をいただければ幸いです。

4

2 に答える 2

0

Since this is a C program, you might have lines as:

{

or

}

ie. opening/closing a block. This is definitely not empty, but it will contain only 1 character. In your while i<6you are going way after the end of this buffer. So, add there a check that i is less than the length of the buffer.

Then it might happen that printf is not necessarily the first expression in the line, such as:

if(something) printf("this");

Your code is not picking this up. You will need to check for the "printf" as a substring in your wd. Look at http://www.cplusplus.com/reference/string/string/find/ for reference on finding strings in a string.

And last but not least, I don't see why you want your line to start with a letter (the check for isalpha). This will fail to change code like

{ printf("this"); }

And the reason that it works for small test files is because highly possibly you wrote them to pass your internal "test" but large files usually contain more widely used printf's.

Also, it is not mandatory that the indentation happens with tabs (\t) it might be simple spaces.

于 2012-06-18T06:58:02.913 に答える
0

この簡単な方法で、私はそれを手に入れました:

string RemovePrintf(string value)
{
     string RemovedPrintf,strP;
     size_t poss;
     value.insert(0," ");//insert a white-space, cause  find method not returning position if it present at begin of string.
     poss = value.find("printf");    // position of "printf" in str
     strP = ""; // get insert whitespace at "printf line".
     strP.resize(strP.length());
     if((int)poss > 0)
      RemovedPrintf.append(strP);
     else
      RemovedPrintf.append(value);
     strP.clear();
     RemovedPrintf.resize(RemovedPrintf.length());
     return RemovedPrintf;
}

これは、小さなファイルと大きなファイルの両方で機能します。ところで、私の質問に答えてくれてありがとう。

于 2012-06-19T12:49:12.197 に答える