2 Replies - 858 Views - Last Post: 11 June 2012 - 11:27 AM Rate Topic: -----

#1 arkantos990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-June 12

Editing text file with c++. Altering front of line number count.

Posted 11 June 2012 - 11:07 AM

I am using a program to convert drawings to cnc code and the files that are output constantly need editing because the program does a poor job of outputting functioning code. the most time consuming edit i have to make each time is running through the text file and editing the line numbers at the start of each line so that they run continuously from one to whenever the lines end, normally i can do this by hand for files with up to 200 or so lines, but the most recent batch of code is around 1500 lines, not something i want to sit through. I am moderately familiar with c++ coding (as familiar as half a semester of engineering coding class can make someone) but i am having a hard time coming to a solution for my needs. The code in essence needs to alter the first four numbers of each line of the file so that they run continuously in order until the end of file. Since each line has text following the numbers i am sort of at a loss about how to work with this file. Here is a link to a google doc with the file that needs to be edited for reference:Google Doc

Any help/suggestions would be greatly appreciated, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Editing text file with c++. Altering front of line number count.

#2 Switters  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 110
  • Joined: 03-June 12

Re: Editing text file with c++. Altering front of line number count.

Posted 11 June 2012 - 11:21 AM

Please post an example of the code you are currently using (using "code" tags), along with any compile errors. You also need to be more specific about what your program inputs and target outputs will be.

What you have provided so far is not enough information for anyone to really help you. Feel free to browse other topics in the forum that show how to pose a question.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Editing text file with c++. Altering front of line number count.

Posted 11 June 2012 - 11:27 AM

Personally, I would write an awk or perl script, but if you want to go down the C++ route, remember that the >> operator on the streams will only read as much as needed to parse the particular type, while the getline() method will read until the carriage return.

So some options are to read an entire line, and then find the first non-numeric character. Output your current line counter, and then output the rest of the string. Another option could be to read in an integer to eat the line number, and then getline() to get the rest of the line. Again output your current line counter, and then output the the rest of the line. And there are various other options like reading the entire file into memory, parsing and twiddling all the line numbers, and then writing the whole file back out.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1