Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,159 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,922 people online right now. Registration is fast and FREE... Join Now!




Opening files on the Command line in Unix-Please help!

 
Reply to this topicStart new topic

Opening files on the Command line in Unix-Please help!, How do i open a file

Tara200
15 Dec, 2007 - 08:17 AM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
CODE
#include <iostream>
#include <fstream> //declares input and output file streams usage
#include <stdlib.h>
#include <assert.h>

using namespace std;
using std::ifstream;
using std::ios;
using std::ofstream;

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

ifstream inFile(argv[1]);

// if (!inFile)
// {
// cerr << "File Not Found" << endl;
// exit(1);
// }

inFile.open ("clients.txt", ios::in);

// string name = "";
// getline( inFile,name );



if(inFile.is_open()== true)
if(inFile.is_open())
if(inFile.is_open()== false)
if(!inFile.is_open())

return 0;


Please ,Please could anyone help with this??? I have updated the code with error checking and i keep getting the output File Not Found. So the error checking is working but still no file opened

Please help!!!

This post has been edited by jjhaag: 16 Dec, 2007 - 01:26 PM
User is offlineProfile CardPM
+Quote Post

Moezzie
RE: Opening Files On The Command Line In Unix-Please Help!
15 Dec, 2007 - 09:32 AM
Post #2

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 44


My Contributions
Basically all you have to do is opening the file, make a loop that reads each line into a string and outputs that string.
You have started out great with your code, just leve the second inFile.open() out.

The purpose of your assignment is probably that you learn file I/O so istead of posting you all the code you need here, i'l give you a link. Its great stuff and easy to understand.
http://www.cplusplus.com/doc/tutorial/files.html

I think you should read it all though and write the code yourself. Once your though with the text its all gonna seem really easy.

Good Luck!


User is offlineProfile CardPM
+Quote Post

Tara200
RE: Opening Files On The Command Line In Unix-Please Help!
15 Dec, 2007 - 12:41 PM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
Thanks for your help but all that tutorial tells you is how to open a file which my code does already, apologies if i hadn't been clear enough with my explanation. What i don't actually know how to do is to output the data stored in the text file onto the screen. I am using a command line interface in Unix so it has to be written as a command line argument. I have used the Output Stream operator but it is producing a blank file where my data should be.

I do not want someone to provide the code for my whole assignment just the code so that i can have access to this Text file called Clients.txt? At present the code i have opens the file but it won't display in the terminal.


Could anyone help please??



User is offlineProfile CardPM
+Quote Post

Tara200
RE: Opening Files On The Command Line In Unix-Please Help!
15 Dec, 2007 - 03:36 PM
Post #4

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
#include <iostream>
#include <fstream> //declares input and output file streams usage
#include <stdlib.h>
#include <assert.h>

using namespace std;
using std::ifstream;
using std::ios;
using std::ofstream;

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

ifstream inFile(argv[1]);

// if (!inFile)
// {
// cerr << "File Not Found" << endl;
// exit(1);
// }

inFile.open ("clients.txt", ios::in);

// string name = "";
// getline( inFile,name );



if(inFile.is_open()== true)
if(inFile.is_open())
if(inFile.is_open()== false)
if(!inFile.is_open())

return 0;

Please ,Please could anyone help with this??? I have updated the code with error checking and i keep getting the output File Not Found. So the error checking is working but still no file opened

Please help!!!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Opening Files On The Command Line In Unix-Please Help!
15 Dec, 2007 - 03:38 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Is the text file in the same directory as the program?
User is offlineProfile CardPM
+Quote Post

Tara200
RE: Opening Files On The Command Line In Unix-Please Help!
15 Dec, 2007 - 03:42 PM
Post #6

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
QUOTE(Amadeus @ 15 Dec, 2007 - 04:38 PM) *

Is the text file in the same directory as the program?


Ahh thankyou Amadeus, and yes it is in the same folder i am working in.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Opening Files On The Command Line In Unix-Please Help!
15 Dec, 2007 - 04:42 PM
Post #7

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
The *nix O/S's are case-sensitive. You ought to be careful that you haven't accidentally typed Clients.txt when the file is actually called clients.txt (or vice-versa)
User is offlineProfile CardPM
+Quote Post

Tara200
RE: Opening Files On The Command Line In Unix-Please Help!
16 Dec, 2007 - 10:50 AM
Post #8

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
have managed to open the file but i don't actually understand what i am doing to be honest. Although i understand the
code now it is there but i copied it from the web! Simply because we have been given course work from Uni that we haven't covered in the course.It was mostly Integer based programs and now we have to use strings, files, structs and enums!! With very little instructions on any of them-so you can see my dilemma. We haven't actually written anything with any of those things in it up until now.

All i want the program to do is perform a 'search' through the data:
to detect clients whose membership is about to run out. Could anyone help me please?? and explain what is going on?

5 clients on file
John Smith
15, Beach Drive, Parkington.
52 weeks from
12 05 2007 // date dd mm yyyy

Bridget Jones
265, London Road, Camberley.
12 weeks from
3 11 2007

Barry Biceps
42, Holmes Place, Newington.
12 weeks from
6 10 2007

Paula Okenfold
113, Surrey Quays, East Ham.
52 weeks from
19 12 2006

Jim Sixpack
21, Brighton Pier, Marylebone.
52 weeks from
5 11 2007

include <iostream>
#include <fstream>


using namespace std;
using std::ifstream;
using std::ios;
using std::ofstream;
using std::ios;

enum Month {Jan, Feb, Mar, Apr, May, Jun, Jul,
Aug, Sep, Oct, Nov, Dec};


int main ()
{

ifstream inFile; // Creates an object infile
inFile.open ("clients.txt"); // Opens the text file clients.txt
if (inFile.is_open()) // Tests that the file opened successfully
// Loop for repetition of reading chars
{
while (inFile.good()) // If it evaluates to true
cout << (char)
inFile.get(); // Get data and display text
inFile.close(); // Close Input stream
}
else
{
cout << "Error opening file"; // Displays error if evaluated to false
}

return 0;
}
User is offlineProfile CardPM
+Quote Post

Bench
RE: Opening Files On The Command Line In Unix-Please Help!
16 Dec, 2007 - 12:07 PM
Post #9

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Files shouldn't be too different from cin and cout. The entire C++ I/O library is designed based on the concept of a 'stream' of bytes. Whether that 'stream' comes from the user tapping keys from the keyboard into the keyboard buffer, or from files on a disk, or even from some other device, the syntax and way you handle that data is the same; The only extra step with a file is ensuring sure that file has been opened when you read from it.

A file ought to be slightly more predictable than user input from the keyboard, which means you can loop through a file grabbing multiple bits of data at a time before your program needs to do anything with that data. Files are sequential, so you always start at the beginning and finish at the end. Which means, if you want line 5 of the file, you need to have read lines 1,2,3 and 4 first.
- There's absolutely no "random access" here - this is why file handling almost always involves a loop somewhere.

- In your case, it looks like you want to read 4 lines of a file in order to construct a record about a person. (The record part is where your struct will come in, though you can build and test the struct without any file or user I/O whatsoever - and i strongly recommend doing it this way, since I/O always adds complexity to a program)

if you've not played around with strings before, look up the <string> library. C++'s string type is designed to be easy to use. Many of the operations you can do with integers, can also be done with strings (such as comparing, copying, etc).


You might want to forget enum's for the moment (They can't help you with file input anyway) - see if you can get your program to read a line of text from your file using the getline function - Remembering that the file syntax is exactly the same as for cin.
CODE
  string mystring;
  getline( cin, mystring );



Once you read a line from the file as text, its up to you to turn it into something meaningful to your program. eg, copying a Person's name into a name variable, and converting a date string into 3 separate numbers - String manipulation is a pretty big topic, you may decide that the layout of the data in your file needs to change in order to make life easier when reading data with your program.

This post has been edited by Bench: 16 Dec, 2007 - 12:36 PM
User is offlineProfile CardPM
+Quote Post

Tara200
RE: Opening Files On The Command Line In Unix-Please Help!
16 Dec, 2007 - 02:15 PM
Post #10

New D.I.C Head
*

Joined: 13 Oct, 2007
Posts: 39


My Contributions
Thankyou Very much Bench!!! I was at a complete loss there! but have managed to get it to cin new data i am working on saving it to a new file now. I think the point of the exercise is to open ,manipulate and save files, So thankyou!

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:46PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month