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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-11-2010, 04:28 PM
|
#1
|
LQ Newbie
Registered: Apr 2010
Posts: 5
Rep:
|
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
|
|
|
04-11-2010, 05:10 PM
|
#2
|
Member
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572
Rep: 
|
erm, maybe I'm missing something here but :
Code:
cat -n file.cpp > file.lst
???
|
|
|
04-11-2010, 05:14 PM
|
#3
|
Member
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572
Rep: 
|
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!).
|
|
|
04-11-2010, 06:04 PM
|
#4
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
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 06:06 PM.
|
|
|
04-11-2010, 06:16 PM
|
#5
|
LQ Newbie
Registered: Apr 2010
Posts: 5
Original Poster
Rep:
|
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
|
|
|
04-11-2010, 06:21 PM
|
#6
|
LQ Newbie
Registered: Apr 2010
Posts: 5
Original Poster
Rep:
|
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
|
|
|
04-11-2010, 06:27 PM
|
#7
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by marzrocks
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 ?
|
|
|
04-11-2010, 06:36 PM
|
#8
|
LQ Newbie
Registered: Apr 2010
Posts: 5
Original Poster
Rep:
|
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 06:41 PM.
|
|
|
04-11-2010, 06:56 PM
|
#9
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
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 07:00 PM.
|
|
|
04-11-2010, 10:26 PM
|
#10
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
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 10:54 PM.
|
|
1 members found this post helpful.
|
04-11-2010, 11:20 PM
|
#11
|
LQ Newbie
Registered: Apr 2010
Posts: 5
Original Poster
Rep:
|
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
|
|
|
04-12-2010, 07:10 AM
|
#12
|
Member
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572
Rep: 
|
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: }
|
|
|
All times are GMT -5. The time now is 08:42 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|