Thread: fstream

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    22

    fstream

    What I'd really like to be able to do is import data from text files into my programs. I am able to do this on Sun terminals at school, but not with Dev-Bloodshed at home. This is an example of a program that should open a text file and print out the numerical data in it. The text file is attached.
    Code:
    #include <iostream.h>
    #include <math.h>
    #include <fstream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    
    int main()
    {
    //Variable IDs
    double a[100];int i,j,N,his[50]={0};
    cout<<"Type in the number of data points"<<endl;
    cin>>N;
    
    //Read in data from file3.dat
    if(N<=64)
    {                                     //Read in data from file3.dat
    ifstream source;
    source.open("file3.dat");
    
    if(!source)
    {cerr<<"Can't open data file."<<endl;
    system("PAUSE");
    return(1);}
    
    for(i=0;i<N;i++)
    {source>>a[i];}
    source.close();
    }
    
    else
    {cerr<<"Too many data points."<<endl;
    system("PAUSE");
    return (1);}
    
    
    //Print out data before sorting
    int k,colnum=8;
    for(j=0;j<N;j=j+colnum)
    {k=colnum;
    if((j+colnum)>N)
    {k=N-j;}
    for(i=0;i<k;i++)
    {cout<<setw(8)<<a[i+j];}
    cout<<endl;}
    system("PAUSE");
    return 0;}

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So what does it do instead of opening and writing?



    P.S: use the new headers.
    <iostream>
    <cmath>
    <fstream>
    <iomanip>
    <cstdlib>
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    source.open("file3.dat");

    It could be that file isn't located in the same directory as your program, so try using the full path of the filename, like:

    "D:\\MyData\\file3.dat"

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    I put the data file in the same folder where the program file is saved, but it just returns (1) with the "Can' open data file." function that is built into the program.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Is there a difference between the project and output directory in bloodshed?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    Don't know. Do you think that I need an additional #include<> statement?

  7. #7
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    with my computer and compiler, even if its the same directory, i have to put a backslash before it. ie.
    "\\textfile.txt"
    see if that works for you

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    22
    What compiler do you use? The backslash didn't work but thanks.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    maybe im just being utterly and completely naive, but i see you have file3.DAT in your code, yet your test file is file3.TXT

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think that's just because .dat files can't be uploaded.

    Don't know. Do you think that I need an additional #include<> statement?
    No, but if you launch the app from within the IDE it might set the working directory to something different (VC++ does).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  2. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  3. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  4. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM