Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Confused as to how to make a .dat file? (To test ifstream/ofstream)

stream

  • Please log in to reply
8 replies to this topic

#1 Steamsteam

Steamsteam

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 23 November 2010 - 10:29 PM

I am not in school which finally became a problem since my text book assumes I am and tells me to get the .dat files from my instructor. Thats so I am able to test the <fstream> and its uses. Unfortunitly as I mentioned, I am teaching my self C++ and not in school. This being said, I have no idea how to make these .dat files my book is refering too. I tried to open a notepad and save it with that extension (after adding a few integers). It became unreadable and I had high hopes. But no such luck. Perhaps my code was wrong? If its not, anyone know how to make a file I can use to test these function calls and streams?

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

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("test.dat");

    int a, b, c, d;

    in_stream >> a >> b >> c >> d;
    cout << a << b << c << d;
}

test.dat is just a file with 1, 2, 3, 4 inside. This program outputs a bunch of random numbers that I cant explain. Maybe you can? ;)

Thanks in advance!
Steam~
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 23 November 2010 - 11:09 PM

You are likely placing characters "1," into an integer throwing it out of range thus the result. You can use ifstream.getline() to separate the stream by comma delimiters instead of spaces, a working example:
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    char numbers[10];

    in_stream.open("test.dat");
    
    if (in_stream.is_open()) {
        while (in_stream.good()) {
            in_stream.getline(numbers, 256, ',');
            cout << atoi(numbers);
        }
        in_stream.close();
    } else {
        cout << "Error opening the dat file";
    }
}

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Steamsteam

Steamsteam

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 23 November 2010 - 11:23 PM

I thought I would give your program a try even though I haven't gotten to the stdlib include directive. Still comes up with random numbers. I have tried many combinations to make the .dat file.
1
2
3
4

Like this one above, with a number on each line

or like this one below, with all the numbers on one line seperated by a comma
1, 2, 3, 4

Not sure if I am doing that right.
  • 0

#4 Settings

Settings

    CC Regular

  • Just Joined
  • PipPipPip
  • 32 posts

Posted 24 November 2010 - 12:36 AM

do you want to make file, open file1 read and write to file2 or just write to file? I ask because I don't understand your problem :rolleyes:
  • 0

#5 Steamsteam

Steamsteam

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 24 November 2010 - 12:40 AM

I want to read from a file then output the results on the screen. The problem is I dont know how to make these files that I can read from. I tried to make a notepad document and save it as .dat and I was able to read from this file, but instead of outputting the 4 numbers 1, 2, 3 and 4 it output some random (or so It seems) result of something like "4818626491". My book just tells me to get the files I require from the teacher (just basic files with basic information, IE numbers and such) but I am teaching my self programing so I need to figure out how to make these files my self. Any ideas?
  • 0

#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 24 November 2010 - 12:53 AM

.dat files can be anything, they are usually plain text files with a dat extension. My code works fine, a sample session in command line:
[linuxbox]$ cat ./test.dat 
1, 2, 3, 4
[linuxbox]$ gcc ./ifstream.cpp -o ifstream
[linuxbox]$ ./ifstream
1234
You must know that the numbers in the stream are actually characters, so they should be stored in a char not an int unless you us atoi().
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#7 Steamsteam

Steamsteam

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 24 November 2010 - 02:08 AM

This is the first im hearing about them being stored as characters and not integers. This is the SAMPLE program that my BOOK shows:
#include <fstream>

int main()
{
    using namespace std;
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("infile.dat");
    out_stream.open("outfile.dat");

    int first, second, third;
    in_stream >> first >> second >> third;
    out_stream << "The sum of the first 3\n"
                     << "numbers in the infile.dat\n"
                     << "is " << (first + second + third)
                     << endl;
    in_stream.close();
    out_stream.close();
    return 0;
}

I copied this exactly as is (with out the documentation) in order to test what I am doing wrong. The input is supose to come from the infile.dat file (and I made a notepad and named it infile.dat and gave it the numbers the BOOK says it is supose to have(1, 2, 3, 4, one on each line)) and I made the outfile.dat file. Left it blank like the program sample program has it. This program is supose to take from infile.dat the first three numbers and sum them up and write it to the outfile.dat file! Still NO luck! I think the problem I am having is how to make these files I can read and write too... I copied this code stright from the book and the only thing I did was try to make the file it reads from and the file it writes too. Sorry for being such a pain ;)

I had to skip ahead 300 pages just to read up on what an atoi is...
  • 0

#8 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 24 November 2010 - 02:17 AM

I was trying to mention how this line works:
in_stream >> first >> second >> third;
This will split each word by whitespace, either space or newline so the following infile.dats will work fine with the code you just posted:
1 2 3 4
or
1
2
3
4
This is possible because the stream splits whitespace by default and can store them in first second and third. If you are going to use something other than whitespace, such as 1,2,3,4 then you must use a bit different method, like my other example with getline();
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#9 Steamsteam

Steamsteam

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 24 November 2010 - 02:34 AM

Ok so I understand that the program automatically understands whitespaces as new integers and that in order to use something like a comma (,) to seperate the numbers I will have to use the command "getline();" with my ifstream command. However, I fixed the code like you said (1 2 3 4) and it still shows no results. When I run the program, the outfile.dat is unchanged and when I change the program to "cout" to test it by sending it to the screen AGAIN it sends messed up information. (something like "192401" instead of "6" (the sum of the first three digits)) Any reason this is happening? (Not writing to the outfile.dat like it is supose too, as well as giving false information)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download