Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to insert a "#" to some lines ?

How to insert a "#" to some lines ?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W wangningyu

    try to use CStdioFile::ReadString to read one line once. :)

    F Offline
    F Offline
    Faez Shingeri
    wrote on last edited by
    #4

    Need to code it in C :)

    D 1 Reply Last reply
    0
    • F Faez Shingeri

      Need to code it in C :)

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #5

      So what do you have so far? You'll find far more people willing to critique existing code than those willing to do your work outright for you.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

      F 1 Reply Last reply
      0
      • D David Crow

        So what do you have so far? You'll find far more people willing to critique existing code than those willing to do your work outright for you.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

        F Offline
        F Offline
        Faez Shingeri
        wrote on last edited by
        #6

        I shall henceforth post the code instantly while asking a new question next time:) BTW here is my code and I think am lost... :sigh:

        #include #include #define MAX_LEN_SINGLE_LINE 120
        #define bufsize 1024

        int main()
        {

         char fileOrig\[32\] = "test.txt";
         char fileRepl\[32\] = "myReplacedFile.txt";
        
         const char text2find\[80\] = " READ ";
         const char text2repl\[80\] = "# READ ";
         int c, line=0;
        
            char buffer\[MAX\_LEN\_SINGLE\_LINE+2\];
            char \*buff\_ptr, \*find\_ptr, \*tok;
            FILE \*fp1, \*fp2;
            size\_t find\_len = strlen(text2find);
        
            fp1 = fopen(fileOrig,"r+");
            fp2 = fopen(fileRepl,"w+");
            int i;
        
        
            while(fgets(buffer,MAX\_LEN\_SINGLE\_LINE+2,fp1))
            {
                buff\_ptr = buffer;
        
               	while ((find\_ptr = strstr(buff\_ptr,text2find)))
               	{
               		while ((c = fgetc(fp1)) == '.')
               		{
               			if (c == '\\n')
               				fputc("#",fp2);
               			else ;
               		}
        
                		while(buff\_ptr < find\_ptr)
                        fputc((int)\*buff\_ptr++,fp2);
        
                	fputs(text2repl,fp2);
        
                    buff\_ptr += find\_len;
        
                }
                	fputs(buff\_ptr,fp2);
        
            }
        
        
        
           fclose(fp2);
           fclose(fp1);
           return 0;
        

        }

        Thanks in advance, Faez

        D 1 Reply Last reply
        0
        • F Faez Shingeri

          I shall henceforth post the code instantly while asking a new question next time:) BTW here is my code and I think am lost... :sigh:

          #include #include #define MAX_LEN_SINGLE_LINE 120
          #define bufsize 1024

          int main()
          {

           char fileOrig\[32\] = "test.txt";
           char fileRepl\[32\] = "myReplacedFile.txt";
          
           const char text2find\[80\] = " READ ";
           const char text2repl\[80\] = "# READ ";
           int c, line=0;
          
              char buffer\[MAX\_LEN\_SINGLE\_LINE+2\];
              char \*buff\_ptr, \*find\_ptr, \*tok;
              FILE \*fp1, \*fp2;
              size\_t find\_len = strlen(text2find);
          
              fp1 = fopen(fileOrig,"r+");
              fp2 = fopen(fileRepl,"w+");
              int i;
          
          
              while(fgets(buffer,MAX\_LEN\_SINGLE\_LINE+2,fp1))
              {
                  buff\_ptr = buffer;
          
                 	while ((find\_ptr = strstr(buff\_ptr,text2find)))
                 	{
                 		while ((c = fgetc(fp1)) == '.')
                 		{
                 			if (c == '\\n')
                 				fputc("#",fp2);
                 			else ;
                 		}
          
                  		while(buff\_ptr < find\_ptr)
                          fputc((int)\*buff\_ptr++,fp2);
          
                  	fputs(text2repl,fp2);
          
                      buff\_ptr += find\_len;
          
                  }
                  	fputs(buff\_ptr,fp2);
          
              }
          
          
          
             fclose(fp2);
             fclose(fp1);
             return 0;
          

          }

          Thanks in advance, Faez

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #7

          How about something a tad smaller, like:

          BOOL bPrefix = FALSE;

          while(fgets(buffer, sizeof(buffer), fp1))
          {
          if (strncmp(buffer, "READ", 4) == 0)
          bPrefix = TRUE;
          else if (buffer[strlen(buffer) - 1] == '.')
          bPrefix = FALSE;

          if (bPrefix)
              fputc('#', fp2);
          
          fputs(buffer, fp2);
          

          }

          I've not tested it, but hopefully you get the idea.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          F 1 Reply Last reply
          0
          • D David Crow

            How about something a tad smaller, like:

            BOOL bPrefix = FALSE;

            while(fgets(buffer, sizeof(buffer), fp1))
            {
            if (strncmp(buffer, "READ", 4) == 0)
            bPrefix = TRUE;
            else if (buffer[strlen(buffer) - 1] == '.')
            bPrefix = FALSE;

            if (bPrefix)
                fputc('#', fp2);
            
            fputs(buffer, fp2);
            

            }

            I've not tested it, but hopefully you get the idea.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            F Offline
            F Offline
            Faez Shingeri
            wrote on last edited by
            #8

            There are no changes in the new file at all :| Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with # I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:

            D 1 Reply Last reply
            0
            • F Faez Shingeri

              There are no changes in the new file at all :| Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with # I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #9

              Faez Shingeri wrote:

              There are no changes in the new file at all :|

              Use the debugger to step through the code line by line to see what is happening along the way.

              Faez Shingeri wrote:

              Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with #

              The code snippet I provided only precedes those lines that start with READ.

              Faez Shingeri wrote:

              I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:

              fgets() internally increments the file pointer.

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

              F 2 Replies Last reply
              0
              • D David Crow

                Faez Shingeri wrote:

                There are no changes in the new file at all :|

                Use the debugger to step through the code line by line to see what is happening along the way.

                Faez Shingeri wrote:

                Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with #

                The code snippet I provided only precedes those lines that start with READ.

                Faez Shingeri wrote:

                I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:

                fgets() internally increments the file pointer.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                F Offline
                F Offline
                Faez Shingeri
                wrote on last edited by
                #10

                Nothing helped... Tried alot of trial and error but the o/p refuses to roll out as expected.. :^)

                1 Reply Last reply
                0
                • D David Crow

                  Faez Shingeri wrote:

                  There are no changes in the new file at all :|

                  Use the debugger to step through the code line by line to see what is happening along the way.

                  Faez Shingeri wrote:

                  Also, there are multiple instance of READ such as END-READ etc whose line should not be precceded with #

                  The code snippet I provided only precedes those lines that start with READ.

                  Faez Shingeri wrote:

                  I aint understanding how the current file pointer can be incremented till a "\n" or "." :confused:

                  fgets() internally increments the file pointer.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  F Offline
                  F Offline
                  Faez Shingeri
                  wrote on last edited by
                  #11

                  It's working great now .. :-D Thnx alot for help

                  D 1 Reply Last reply
                  0
                  • F Faez Shingeri

                    It's working great now .. :-D Thnx alot for help

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #12

                    So what did you end up with (to get it all going)?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    F 1 Reply Last reply
                    0
                    • D David Crow

                      So what did you end up with (to get it all going)?

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                      F Offline
                      F Offline
                      Faez Shingeri
                      wrote on last edited by
                      #13

                      while(fgets(buffer,MAX_LEN_SINGLE_LINE+2,fp1))
                      {
                      buff_ptr = buffer;
                      strcpy(buffer2,buffer);
                      //Remove Spaces from start for comparision
                      i = 0;
                      while( i++ < strlen(buffer))
                      {
                      if(isspace(buffer[i]))
                      continue;
                      strcpy(buffer2,&(buffer[i]));
                      break;
                      }
                      if(strncmp(buffer2,"READ",4) == 0)
                      {
                      found = true;
                      sprintf(buffer1,"# %s",buffer);
                      fputs(buffer1,fp2);
                      }
                      else if(found == false)
                      {
                      fputs(buffer,fp2);
                      }
                      else
                      {
                      sprintf(buffer1,"# %s",buffer);
                      fputs(buffer1,fp2);
                      // Search if "." is found.
                      if(strstr(buffer,".") != NULL)
                      found = false;
                      }
                      }

                      Alot of thanks to Chandrakantt also in helping me in this one..

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups