First real Program!!!!!!!!!!!

Background:
I switched to the new linux ubuntu which wouldnt work with my wifi card for some reason but because it had c and c++ compilers i was too lazy to switch back. the only problem was whenever i made a mistake it was like at line 58 or 129. and because i couldnt download an ide i had to count it by hand so i made a program that can do one of three things depending on its arguements.
1) count the total lines in a program
2) find a specified string and tell you the line its on
3) input a number and it will tell you what that line is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
	ifstream file;
	unsigned long lineNumber = 0;
	string line;

	if(argc == 1)
		cout<<"error: too few arguements\n";

	else if(argc == 2)
	{
		file.open(argv[1]);

		while(getline(file, line))
		{
			lineNumber++;
		}

		cout<<"Total lines: "<< lineNumber << endl;
	}

	else if(argc == 3)
	{
		file.open(argv[1]);

		if(atoi(argv[2]))
		{
			while(getline(file, line))
			{
				lineNumber++;

				if(lineNumber == atoi(argv[2]))
				{
					cout<<"line "<< lineNumber <<": "<< line << endl;
					break;
				}

				else if(lineNumber != atoi(argv[2]) && file.eof())
					cout<<"invalid line index by "<< argv[2] - lineNumber << endl;
			}
		}

		else
		{
			while(getline(file, line))
			{
				lineNumber++;

				if(line == argv[2])
				{
					cout<<"line \""<< line <<"\": "<< lineNumber << endl;
					break;
				}

				else if(file.eof())
					cout<<"invalid line: "<<  line <<"not found"<<endl;
			}
		}
	}
}
Did I miss the question?
It's a show off.
first its not a question thats why its in lounge and its not a show off its excitement
It looks nice. What happens if I only want to find what's at line n? Sounds like I'd still need to put in 3 arguments.

Something that would be pretty fun to do, is actually break this into three different programs and turn them into programs that your shell can call. Adding shell commands is pretty fun, and this sounds like a useful one. Though I'm not sure if you're duplicating anything *nix already provides.
One possibility would be:

cat filename | awk 'NR==149'

Where 149 is the line number.

All the best,
NwN
To count the lines in a file, you can use wc; to find a string you use grep (it doesn't tell you the line number but it does display the whole line). I don't know about the third thing.
So let me get this straight...you were so lazy that you decided to write a program that lets you do it the hard way. Excellent choice :)
Sounds like pretty typical programmer behaviour to me: "Rather than figure out how to use these pre-supplied tools the way the developers intended, I'll just make my own ones". I once spent a day writing a basic version of an X Window System tool called mkfontdir because I couldn't find one online (in about two minutes of searching -- secretly I think I just wanted to do it myself) and my system didn't seem to have one. I don't even know exactly what mkfontdir does or what it's for (I know it generates fonts.dir files but I don't know what they are or what they do), I just had the man page. It didn't work in the end and I can't even remember why I needed it, but yeah, IMO doing stupid things like that is normal.
Last edited on
grep --line-number
or nl file | grep

head --lines=K | tail --lines=1

What happens if I only want to find what's at line n? Sounds like I'd still need to put in 3 arguments
You could use flags. Take a look at getopt()
mine you only need two or one args lineFind fileName will tell you how many lines are in a file. lineFind fileName int will tell you whats on line int. lineFind filename string will tell you what line what line string is on. and yes i know there were pre made tools for this but i just felt like making my own so i could have it in one program
I switched to the new linux ubuntu which wouldnt work with my wifi card for some reason


That's easy to fix, all you need to do is go into the terminal thing, type 3 pages of text (you can find it in google) and hit the enter key. Just make sure you didn't mistype anything, or you might trash your operating system. There's also the slim possibility of losing some of your hair over the ordeal.

Now that I'm done ripping on both Linux customer and hardware support, I'll contribute something useful:

You might consider printing the intended usage if an invalid number of arguments are provided. Black box utilities are only as useful as they are intuitive, so instead of "error: too few arguments", you could perhaps print out an intuitive course of action for the end-user to take in order to resolve their misunderstanding.
Topic archived. No new replies allowed.