calling text file in command line

Heres my question, we are required to call any text file using the command line, without using the string class #include<string.h>, so i cant use getline(). How are you supposed to do this. Really need help, thanks.
closed account (EvoTURfi)
http://www.cplusplus.com/reference/iostream/istream/getline/

There are two getlines. One for strings and another for character arrays. The getline the link goes to is the one for character arrays. You can use it without the strings library. This one is cin.getline.
Last edited on
Do you need to be input during runtime or will the user need to run the program with the file as an argument?
I need to run the program with the file as an argument.
closed account (EvoTURfi)
http://www.cprogramming.com/tutorial/lesson14.html

Use this tutorial.
Last edited on
Thanks for the tutorial, but i have one more question. Everything compiles but when i try adding the text file, as many different ways that i have written the code it always gives me the declaration syntax error.
so i get

C:\users\username>bcc32 program.cpp text.txt

borland c++ 5.5.1 for win32 copyright <c> 1993, 2000 Borland
program.cpp:
text.txt:
error e2141 add.txt 1: declaration syntax error
***1 errors in compile ***

Any idea of what is going wrong?
closed account (EvoTURfi)
@efigen

Post the code. I'll see if I can help.
This is only part of the program, the rest i have not finished, but need to get this first.
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
//Run in Command Line

#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string numbers;
	char *number=0;
	string filename;
	ifstream inputfile;
	ofstream outfile("sum.txt");

	filename=argv[1];

	if (argc>0)
	{
	ifstream inputfile (filename);
	inputfile.open(filename);

	if(inputfile.is_open())
	{
		ofstream outfile("sum.txt");
	}
	while (!inputfile.eof())
	{
		cout<<argv[1];		
	}
	inputfile.close();
	}
	else 
	{
	ofstream outfile("sum.txt");
	cout<<"Invalid Sequence"<<endl;

}

	}
Ok nevermind i got it, reason was how i was running the program
in the command line shoulda been with the .exe file after starting compiler and then the .txt file.
No what i do is take the data in the text file which should be integers and then add them, after that i pass the sum to the sum.txt file. I realized there was some errors in my code so i fixed it
So in the while loop i would do the sum stuff.
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
//Esteban Efigenio
//Run in Command Line
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string filename;
	fstream inputfile;
	ofstream outfile;
	argv[1]="filename";


	if (argc>0)
	{
	fstream inputfile;
	inputfile.open(argv[1],ios::in);

	if(inputfile.is_open())
	{
		ofstream outfile;
		outfile.open("sum.txt", ios::app);

	char ch[256]={0};
	while(!inputfile.eof())

	for (int i=0;i<256;i++)
	if(ch[i]>char(32) && ch[i]< char(127) && ch[i]!=' ')
	{
	inputfile>>argv[1]>>ch[i];
	sum+=(int)ch[i];
	outfile<<"sum= "<<sum<<endl;

	inputfile.close();
	outfile.close();
	exit(0);	
	}

	}
	else if(inputfile.fail())
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (1);

}

	}

}
Last edited on
closed account (EvoTURfi)
It's ofstream, not fstream. Also, I think that you can just put #include<fstream> instead of including both fstream and iostream. The reason is that if you look at the hierarchy:

http://www.difranco.net/cop2334/Outlines/ch21-1.gif

fstream is on the bottom, below iostream. This means that fstream includes everything iostream has.
Thanks removed iostream, for some reason i always add it. i just have one last problem in my code that i cannot figure out.

In my while loop after i open the input file, i want it to read the file and if it has any characters that are not numbers, so it cannot have any spaces in between numbers without an addition sign in between (valid 1+3+5+6, invalid 1+4 5+7, invalid A+B+3+4) to exit the loop and show an invalid sequence in the output file, if it is valid i want it to do the summation. Here is the code
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
//Run in Command Line
#include<fstream>
#include<iomanip>

using namespace std;

int main (int argc,char *argv[])

{
	int sum=0;
	string filename;
	ifstream inputfile;
	ofstream outfile;
	argv[1]="input=<filename>", "sum input=<filename>";

	if (argc>0)
	{
	ifstream inputfile;
	inputfile.open(argv[1],ios::in);

	if(inputfile.is_open())
	{
		ofstream outfile;
		outfile.open("sum.txt", ios::app);
	int i=0;
	char ch[]={0};
	while(!inputfile.eof())
	ch[i]=inputfile.peek();
	inputfile>>ch[i];
	for (i=0;i<256;i++)
	if(ch[i]<char(32)|| ch[i]>char(127) || ch[i]!=' ')
	{
		
	sum+=(int)ch[i];
	outfile<<"sum= "<<sum<<endl;

	inputfile.close();
	outfile.close();
	exit(0);	
	}

	}
	else if(inputfile.fail())
	{
	ofstream outfile;
	outfile.open("sum.txt", ios::app);
	outfile<<"Invalid Sequence"<<endl;
	outfile.close();
	exit (1);

}

	}

}
closed account (EvoTURfi)
Why are your redeclaring argv[1] when the user will pass the arguments through the command line?
Also, put brackets after the while loop declaration.

while(!inputfile.eof())
Last edited on
Thanks im new to this command line stuff, I have one more problem, i have to put some special cases in there like it cannot allow (1+2+A), so it can not allow Letters, and spaces between numbers like this (1+2 5+4) if these happen then i write invalid sequence to the output file. Help please.
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
   //Run in Command Line
   #include<iostream>
   #include<fstream>
   #include<iomanip>
    
   using namespace std;
    
   int main (int argc,char *argv[])
    
   {
   int sum=0;
   int x=0;
   char sign='+';

    
   string filename;
   std::ifstream inputfile;
   std::ofstream outfile;
    
   if (argc>1)
   {
   inputfile.open(argv[1], ios::in);
    
   if(inputfile.is_open())
   {
   outfile.open("sum.txt", ios::app);
    
    
   while(!inputfile.eof() && !inputfile.fail())
   {
   inputfile>>x>>sign;
   if(sign=='+')
   {
   sum+=x;
   }
else if ((sign>(char)31)&&(sign<(char)128))
{
inputfile.fail();
}
   }
   cout<<sum;
   outfile<<"sum= "<<sum<<endl;
    
   inputfile.close();
   outfile.close();
   exit(0);
   }
    
   }
   else if(inputfile.fail())
   {
   ofstream outfile;
   outfile.open("sum.txt", ios::app);
   outfile<<"Invalid Sequence"<<endl;
   outfile.close();
   exit (1);
    
   }
    
   }
nevermind i figured it out, YAY!!!!!!!!
Topic archived. No new replies allowed.