Welcome to Dream.In.Code
Become a C++ Expert!

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




Trouble opening a file

 
Reply to this topicStart new topic

Trouble opening a file, the file is not being recongnized or opened

goblue89x
13 Mar, 2008 - 08:06 PM
Post #1

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 8


My Contributions
I am currently writing a program that opens a file of data and prints it to the screen. But for some reason when i try to open the file it doesn't work and thats kind of a important part of this program..

not sure if i am missing something or should i save the file in a different format?

CODE
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    string filename = "A5.txt";
char ch;
long offset, last;
ifstream inFile(filename.c_str());

if (inFile.fail())
{
cout<<"\nThe file was not successfully opened"<<endl;
cin.ignore();
exit(1);
}

inFile.seekg(0L,ios::beg);
last = inFile.tellg();

for(offset = 1L; offset <= last; offset++)
{
inFile.seekg(-offset, ios::end);
ch = inFile.get();
cout<<ch<<" "<<endl;
}

inFile.close();
cout<<endl;

cin.ignore();cin.ignore();
}

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Trouble Opening A File
13 Mar, 2008 - 08:13 PM
Post #2

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Your code to open the file is working, I mean, I do not receive the output "The file was not successfully opened". Check if the file is placed in the same folder as the .cpp file and also check the names.

Also, the printing of the file contents isn't working. But that is for some other post.

Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

goblue89x
RE: Trouble Opening A File
14 Mar, 2008 - 03:25 PM
Post #3

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 8


My Contributions
oh i didnt have the text file in the same folder as the .cpp file. thanks!! it opened, but now i got the issue of reading the contents lol..

whats a basic code for displaying it
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Trouble Opening A File
14 Mar, 2008 - 04:11 PM
Post #4

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Well, you will have to explain what you need to do. Do you just want to print the file on the screen, or get only specific information from the file?



If you just need to print the contents of the file as output:

http://cplusplus.com/reference/iostream/istream/get.html

That should be helpful.
User is offlineProfile CardPM
+Quote Post

goblue89x
RE: Trouble Opening A File
14 Mar, 2008 - 06:04 PM
Post #5

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 8


My Contributions
now im at this

CODE

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    string filename = "A5.txt";
    string filename2 = "A5N.txt";
    char ch;
    ifstream inFile;
    ofstream outFile;
string line, name, address;
int age, Socialnum;

inFile.open(filename.c_str());
inFile>>name>>age>>Socialnum>>address;
while (inFile.good())
{
cout<<name<<endl<<age<<endl<<Socialnum<<endl<<address<<endl;
inFile>>name>>age>>Socialnum>>address;
}

try
{
outFile.open(filename.c_str());
if (outFile.fail()) throw filename;
            
inFile.close();
outFile.close();
}
catch (string in)
{
cout<<"The input file"<<in<<"was not successfully opened."<<endl;
cout<<"The new file wasn't made";
exit(1);
}
cout<<"A successful backup of "<<filename<<" named "<<filename2<<" was successfully made."<<endl;



cin.ignore();cin.ignore();
}


and it doesnt come up showing my contents of the file.

my file is in the format of
Name
Age
SS Number
address

but that information for several people

example of one:
April,Joe
21
312033387
126 Freelane, Yourtown, State 88990


and the site didnt help me much, thanks though for trying, im not sure which codes i need to use to display this right

User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Trouble Opening A File
14 Mar, 2008 - 07:08 PM
Post #6

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Well, here is some of the code that I was able to come up with for today:

CODE

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    string filename = "A5.txt";
    string filename2 = "A5N.txt";
    //char ch;
    ifstream inFile;
    ofstream outFile;
    string line, name, address,temp;
    int age, Socialnum;

    inFile.open(filename.c_str());
    if(inFile.is_open()){

        while (!inFile.eof()) {

            getline(inFile,name); //a way to read strings
            
            inFile>>age;
            getline(inFile,temp); //moving the getline pointer
            
            inFile>>Socialnum; getline(inFile,temp);
            
            getline(inFile,address); getline(inFile,temp);
            
            cout<<name<<endl<<age<<endl<<Socialnum<<endl<<address<<endl;    

        }        
    }
    else{
        cout << "File Could not be opened!";
    }
    try
    {
        outFile.open(filename2.c_str(),ios::trunc);
        if (outFile.fail()) throw filename;
            outFile<<"Hello World!";
        inFile.close();
        outFile.close();
    }
    catch (string in)
    {
        cout<<"The input file"<<in<<"was not successfully opened."<<endl;
        cout<<"The new file wasn't made";
        exit(1);
    }
        cout<<"A successful backup of "<<filename<<" named "<<filename2<<" was successfully made."<<endl;

    cin.ignore();cin.ignore();
    return 0;
}

User is offlineProfile CardPM
+Quote Post

goblue89x
RE: Trouble Opening A File
14 Mar, 2008 - 09:33 PM
Post #7

New D.I.C Head
*

Joined: 5 Mar, 2008
Posts: 8


My Contributions
ok that program got me two lines of some random numbers that don't match to any of my numbers in my list. then the back up message at the end.

i don't get why this file wont read easily
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Trouble Opening A File
16 Mar, 2008 - 04:34 AM
Post #8

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
With this code and the attached input file, I was able to get the output below:

CODE

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    string filename = "A5.txt";
    string filename2 = "A5N.txt";
    //char ch;
    ifstream inFile;
    ofstream outFile;
    string line, name, address;
    char temp[256];     //temp char array for strtod
    int age, Socialnum;

    inFile.open(filename.c_str(),ios::in);
    if(inFile.is_open()){

        while (!inFile.eof()) {

            getline(inFile,name);            
            inFile.getline(temp,256);
            age = (int)strtod(temp,NULL); //string to double and casting to int
            inFile.getline(temp,256);
            Socialnum = (int)strtod(temp,NULL);
            getline(inFile,address);
            cout << name << endl << age << endl << Socialnum << endl << address << endl;

        }        
    }
    else{
        cout << "File Could not be opened!";
    }
    try
    {
        outFile.open(filename2.c_str(),ios::trunc);
        if (outFile.fail()) throw filename;
            outFile<<"Hello World!";
        inFile.close();
        outFile.close();
    }
    catch (string in)
    {
        cout<<"The input file"<<in<<"was not successfully opened."<<endl;
        cout<<"The new file wasn't made";
        exit(1);
    }
    cout<<"A successful backup of "<<filename<<" named "<<filename2<<" was successfully made."<<endl;

    cin.ignore();cin.ignore();
    return 0;
}

CODE
April,Joe
21
312033387
126 Freelane, Yourtown, State 88990
John, Doe
43
321353135
177 Broadway, New York City, New York 13203
A successful backup of A5.txt named A5N.txt was successfully made.

Press any key to continue


I hope you will get further with the changes in the file input code :)


Attached File(s)
Attached File  A5.txt ( 132bytes ) Number of downloads: 14
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 09:47PM

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