LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-11-2010, 03:28 PM   #1
marzrocks
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Rep: Reputation: 0
Create File Listing in C++ that will generate a line number on every line of code


Hi,
I have a project due for my Intro to C++ class and we are suppose to generate a file listing that will take an input of a C++ source code with .cpp extension and make a copy of it with a .lst extention that will have a line number preceding each and every line.

More details about the project is here:
http://staffwww.fullcoll.edu/sedward...leListing.html

I have no idea how to start this and I will appreciate any sort of help at all.

Thanks,

Marz
 
Old 04-11-2010, 04:10 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
erm, maybe I'm missing something here but :

Code:
cat -n file.cpp > file.lst

???
 
Old 04-11-2010, 04:14 PM   #3
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Ah sorry, just realised you are meant to write this in c++ lol!
Well, the simplest way is a simple file read line by line and a variable which you increment before each read and then print at the start of the line (printf may be your friend!).
 
Old 04-11-2010, 05:04 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

If you needed this in "the real world", and your platform happened to be Linux, the "cat -n file.cpp > file.lst" might well be the best solution. Or simply start your favorite text editor (for example, "vi") or IDE (for example, "Eclipse/CDT") and print line numbers. Easy! Done!

Since this is a class assignment, your program needs to do the following:
Code:
  1. Open the input file
  2. Open the output file
  3. Loop until end-of-input-file
  3.   Read the next line from the input file
  4.   Write the next line (including a line#) to the output file
  5. Close both output and input files when done
I would look up the following C++ constructs (look in your textbook, or use Google):
Code:
  * "main (int argc, char *argv[])":
    <= You will get your input filename from here

  * "cin", "cout": 
    <= You will read and write using these C++ "stream objects" 

  * You'll also need some "int line_no" to keep track of the current line#
'Hope that helps .. PSM

PS:
Here are two good tutorials:
http://www.cplusplus.com/doc/tutorial/files/
http://www.daniweb.com/forums/thread6542.html

Last edited by paulsm4; 04-11-2010 at 05:06 PM.
 
Old 04-11-2010, 05:16 PM   #5
marzrocks
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for the replies. I'm gonna start working on the codes and post it up here to see if I got it right. I know what to do, but I'm not sure how to write the code so I'm going to look at the tutorials and see what I can do

Thanks again,

Marz
 
Old 04-11-2010, 05:21 PM   #6
marzrocks
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Actually,
I have a question that right to my mind before even starting to write the code. I know how to write code to open up a certain file but my professor wants us to only open any file with .cpp and the file name can only have letters, digits and underscores. Also, how do I convert the .cpp file to .lst file in the code?

Thanks in advance,

Marz
 
Old 04-11-2010, 05:27 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by marzrocks View Post
Actually,
I have a question that right to my mind before even starting to write the code. I know how to write code to open up a certain file but my professor wants us to only open any file with .cpp and the file name can only have letters, digits and underscores. Also, how do I convert the .cpp file to .lst file in the code?

Thanks in advance,

Marz

Files can be opened for reading and for writing. So, what did you professor say about opening files for writing ?
 
Old 04-11-2010, 05:36 PM   #8
marzrocks
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
What he wants us to do is to open up any C++ source code file with .cpp extension and then have the program create a copy of the source code file but it has .lst extension instead and then it tell the user how many lines it has and the program ends and when u open up the .lst that was created with a text editor the new file should look like this:

Code:
0000 // ===============
0001 // #include <iostream>
0002 // using namespace std;
0003 //
0004 // int main ()
I have no idea how to start writing this code and its due tomorrow morning.

Last edited by marzrocks; 04-11-2010 at 05:41 PM.
 
Old 04-11-2010, 05:56 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again:

1. In general please, make it a habit to use "int main (int argc, char *argv[])" instead of "int main()". I know somebody's going to raise their hand and flame my, but I honestly feel it's just a good convention. IMHO...

2. Your question:
Quote:
Q: How do I convert the .cpp file to .lst file in the code?
3. Again (at the risk of being repetitive):
Code:
// MAIN ALGORITHM
  1. Open the input file
  2. Open the output file
  3. Loop until end-of-input-file
  3.   Read the next line from the input file
  4.   Write the next line (including a line#) to the output file
  5. Close both output and input files when done
4. There are several implicit steps involved here. For example, something you need to do for step 2) is:
Quote:
"Given the .cpp filename, convert it into a .lst file name"
Another implicit step might be:
Quote:
"Get the .cpp filename from the command line, and make sure that it's a valid name".
Please read the three links I posted, please try a few different PARTS of the program (*not* the whole thing, all at once!), AND post back specific code examples. Show what you've tried, and ask any questions.

'Hope that helps .. PSM

Last edited by paulsm4; 04-11-2010 at 06:00 PM.
 
Old 04-11-2010, 09:26 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again -

Even though this is "homework", and there are rules about "doing a student's homework for him" (the rule is: we *don't* ) ...
... please feel free to ask any questions you want, and please be sure to post the work you've done so far when you do so.

ANYWAY:
1. I assume you've already successfully compiled and run a C++ "hello world". Here is an example:
Quote:
vi test1.cpp
Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int
main (int argc, char *argv[])
{
  cout << "Hello world\n";
  return 0;
}
Quote:
$ g++ -Wall -g -o test1 test1.cpp

$ ./test1
Hello world
2. Here an an example that uses the C++ "string" class to see if a filename has a ".cpp" and, if so, returns its "basename":
Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string
get_cpp_basename (string fname)
{
  int pos = fname.find(".cpp");
  if (pos < 0)
    return string ("");
  else
    return fname.substr (0, pos-1);
}

int
main (int argc, char *argv[])
{
  if (argc != 2)
  {
    cout << "Please enter a filename!\n";
    return 1;
  }

  string basename = get_cpp_basename (string(argv[1]));
  if (basename.size () == 0)
  {
    cout << "ERROR: " << argv[1] << " is not a valid .cpp file!\n";
    return 1;
  }
  else
  {
    cout << "basename: " << basename << endl;
    return 0;
  }
}
'Hope that helps .. PSM

PS:
Even something as simple as the example above has several problems with it. For example, it will definitely distinguish between "hello.cpp" vs. "hello.x" vs "hello" (the 1st will succeed, the 2nd two will fail. Just like you want).
... but ...
Something like "cpp.hello" will also succeed ("find()" doesn't care if the substring is that the beginning, end or the middle).

And something like "HELLO.CPP" will fail (case sensitivity).

ANYWAY: good luck, and please feel free to post any questions.

Last edited by paulsm4; 04-11-2010 at 09:54 PM.
 
1 members found this post helpful.
Old 04-11-2010, 10:20 PM   #11
marzrocks
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Hey paulsm4,
Omg you have no idea how much you have helped me write now especially with the second code. I'm still extremely stuck on writing this program and actually was about to seek some help. I'm very new to C++ and I still have a lot to learn to writing codes.
Now I have somewhat of an idea to write the first part of the code. Thank you so much again. If I any questions before tomorrow morning, because thats when its due, I will post it up here. I'll also post up what I've been able to do so far and see if I did it correctly.

Thanks again,

Marz
 
Old 04-12-2010, 06:10 AM   #12
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
You may also want to first read the total number of lines in the file so that you can determine the left hand padding size.
Ie, if you have 5 lines then you don't need to pad however if you have 5000 then it will look ugly not doing:

Code:
1: int main(int argc, char **argv) {
2:   printf("Hello World\n");
3:   
4:   return 0;
5: }
is ok but you want this for more lines:

Code:
   1: int main(int argc, char **argv) {
   2:   printf("Hello World\n");
3000:   
4000:   return 0;
5000: }
and not

Code:
1: int main(int argc, char **argv) {
2:   printf("Hello World\n");
.
.
.
3000:   
4000:   return 0;
5000: }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort by line size (number of characters in a line) fast_rizwaan Linux - General 8 01-08-2010 05:53 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration