Computer problems,Computer help
*AX SOFT>>>Programming & Design

I/O files help with C++ programing?


I am writing a program and I am supposed to ask the user for the name of a file. The 1st line of the file given contains the name of a 2nd file. I need to read the 1st line of the file given by the user to open th 2nd file. How do I do this?
Please help!!

You should probably add some error handling but this code will work fine if everything is set up perfectly (e.g. the files exist, user enters proper information, file data is correct, etc.). Here's my attempt:

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

int main ()
{
string filename1, filename2;

cout << "Enter the file name: ";
cin >> filename1;

ifstream infile1, infile2;
infile1.open(filename1);
getline(infile1, filename2);
infile1.close();

infile2.open(filename2);
// do stuff with file#2 here
infile2.close();

return 0;
}

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • BASH - How To (Replace specific text within a file)?

    #!/bin/bash time=14400 path=/somepath/somedirectory sed -e "s!%time%!$time!; s!%path%!$path!" template.txt > file.txt

    ...
  • Best practice looping through database columns or hardcode columnnames?

    Looping through a database by far is better. Because then if you want to expand you already have an object ready to handle it. Dynamic is the way to go.

    ...
  • Order online ! help.?

    You need a FormMail script. You just insert your e-mail address and the name of the Order Form File that you will design to accept details of your customer's order, and you will receive an e-...

  • How to terminate a Java while loop using a string?

    Your while condition will always evaluate to true. The expression str != "done" asks whether the reference str is different from the reference "done" - that is, do these ...

  • How do you automatically load a javascipt?

    Put the javascript in a file and in a subfolder of your project and then reference it in the header Example: <script language="javascript" src="scripts/general.js"><...

  • Is there any basic principles or codes needed to design a web page?

    Checkout AceHtmlPro (Free Edition) which will help you learn html and allows for easy page generation. Great for anyone starting out.

    ...
  • Dreamweaver MX: Why has the background & links disappeard on my 3rd page when I link to it from my homepage?

    Where exactly are you storing the specification of your links? In each page? Or in a separate style sheet (CSS)? If you're not using an external style sheet, I highly recommend starting to do ...

  • Unix help!!?

    Files in Unix filesystems use inodes as structures that hold the actual file data on disks. inode number uniquely identifies a physical file on disk. Hard links are pretty much multiple names fo...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster