Like Tree1Likes

I/O files: reading, manipulating and outputting data

This is a discussion on I/O files: reading, manipulating and outputting data within the C++ Programming forums, part of the General Programming Boards category; Sorry CornedBee, but I am real stuck here! I did look at numbers.dat and created the necessary separators (snippet below). ...

  1. #16
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Sorry CornedBee, but I am real stuck here! I did look at numbers.dat and created the necessary separators (snippet below). However, in spite of my research at a couple of discussion boards, I am unable to get a handle of how to use std::string that you recommended to be able to open numbers.dat. Sadly, even my C++ book does not address this issue. I know you have already walked me this far, but I will appreciate even more if you give me a clue of what I have to do to use std::string to solve the subject problem. What do I do with std::string in this case? BTW, the limitation imposed by use of the 15 characters originated from the source of this problem example - Walter Savitch. I trust, if I find my way around this problem, I will be able to contemplate better ways to solve similar problems in future. Many thanks and sorry for all the trouble!!

    Code:
    #include <fstream>
    #include <string>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int num1, num2, num3, num4;
        //string numbers;
        cout << "Enter the four numbers: " << endl;
        cin >> num1 >> num2 >> num3 >> num4;
        cout << "The numbers are: " << num1 << " "<< num2 << " " << num3 << " " << num4;
        ofstream writer("numbers.dat");
        if(!writer)
        {
            cout << "Error opening file for output" << endl;
            return -1;
        }
        writer << num1 << ", " << num2 <<", " << num3 <<", " << num4 << endl;
        writer.close();
        return 0;
    }

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Here's how I suggest you read the filenames:
    Code:
    std::string filename;
    std::getline(std::cin, filename);
    std::ifstream input(filename.c_str());
    This allows the filename to be any length and contain spaces. (I'm somewhat doubtful about the quality of the book you're using if it used raw char arrays by the way.)

    Second, I suggest you only use spaces as separators. The commas will be in the way.

    Third, if your second program still can't find the file, you need to understand the concept of a program's "current working directory" and relative paths. This is something that is largely hidden from today's desktop users, is very relevant when you work on the console, and also matters to programs. If you want to be a programmer, this is something you need to learn, even though authors usually tend to forget that today's computer users don't learn this anymore and therefore don't write about it.
    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. #18
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Thank you very much CornedBee for seeing me through this. I really appreciate. With all your insight and a little more online research, I now have the exact solution to my problem. By inputting in the appropriate directory string in the second program (I had to discard the old one and adopt a completely new approach as you advised!), I have a program that open numbers.dat and manipulates the contents as required of it, and then outputs on the screen the results.

    Here below is the new program and it works real fine. Again, thank you very much!

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <cstdlib>
    #include <dirent.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    using namespace std;
    int main()
    {
    ifstream finput;
    string dir, filepath;
    int num1, num2, num3, num4;
    DIR *dp;
    struct dirent *dirp;
    struct stat filestat;
    cout << "dir to get files of: " << flush;
    getline( cin, dir ); // gets everything the user ENTERs
    dp = opendir( dir.c_str() );
    if (dp == NULL)
    {
    cout << "Error(" << errno << ") opening " << dir << endl;
    return errno;
    }
    while ((dirp = readdir( dp )))
    {
    filepath = dir + "/" + dirp->d_name;
    // If the file is a directory (or is in some way invalid) we'll skip it
    if (stat( filepath.c_str(), &filestat )) continue;
    if (S_ISDIR( filestat.st_mode )) continue;
    // Endeavor to read a single number from the file and display it
    finput.open( filepath.c_str() );
    if (finput >> num1 >> num2 >> num3 >> num4)
    cout << filepath << ": " << num1 << " " << num2 << " " << num3 << " " << num4 << " "
    << num1+num2+num3 << endl;
    finput.close();
    }
    closedir( dp );
    return 0;
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data from files
    By heinrichxs in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2011, 01:13 PM
  2. reading data fom files
    By shuo in forum C++ Programming
    Replies: 11
    Last Post: 10-22-2007, 01:24 AM
  3. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 08:12 PM
  4. Replies: 1
    Last Post: 10-22-2005, 06:28 AM
  5. Reading data from files
    By ChwanRen in forum C Programming
    Replies: 4
    Last Post: 05-06-2003, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21