Help with I/O

This is a discussion on Help with I/O within the C++ Programming forums, part of the General Programming Boards category; I am trying to get a program to write to multiple user selected files. I input the file names as ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    5

    Help with I/O

    I am trying to get a program to write to multiple user selected files. I input the file names as an array of strings, but MVS doesn't let me use the array (or a string for that matter) as a file name. Here is a sample of my problem:

    Code:
    	vector<string> files(filenum);
    	cout<<"Please enter the name of the file(s) you wish to convert"<<endl;
    	for (int y=0;y<filenum;y++){
    		cin>>files[y];
    	}
    	for (y=0;y<filenum;y++){
    		ofstream openhtml(line);
    	}
    Sorry if this is a stupid question, but I'm all out of ideas. Thanks!

    edit=code tags
    Last edited by ericj551; 08-23-2002 at 01:31 PM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I've seen wierd stuff with vector in this kind of situation. Perhaps a cast will be necessary?

    cin >> (string)files[y];

    Let me try it out here. I'll get right back to you

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    seems to work for me. Maybe you should output the names just to be sure.

    cout << files[y].c_str() << endl;

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    5
    Thanks for your help, but I'm still getting the same error. Maybe if you saw the entire program you would see if I was doing something wrong...

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
     
    using namespace std;
     
    int main()
    {
    	ifstream ifs("border.txt");
    	string line;
    	vector<string> copy(1024);
    	int x=0;
    	while(getline(ifs,line)) {
    	copy[x]=line;
    	cout<<copy[x]<<endl;
    	x++;
     }
    	cout<<"How many files do you want to update the borders for?-->";
    	int filenum=0;
    	cin>>filenum;
    	vector<string> files(filenum);
    	cout<<"Please enter the name of the file(s) you wish to convert"<<endl;
    	for (int y=0;y<filenum;y++){
    		cin>>files[y];
    	}
    	for (y=0;y<filenum;y++){
    		cout << files[y].c_str() << endl;
    		ofstream openhtml(line);
    	}
    
    	
     return 0;
    }
    and here is the error message if it helps:

    C:\Project Data Files\Tools\onemoretry\mytool.cpp(29) : error C2664: '__thiscall std::basic_ofstream<char,struct std::char_traits<char> >::std::basic_ofstream<char,struct std::char_traits<char> >(const char *,int)' : cannot convert parameter 1 from
    'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    Thanks again

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that error is on the last line in the output loop

    ofstream openhtml(line.c_str());

    or

    ofstream openhtml(files[y].c_str());

    will work.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    5
    Thanks, it worked great, now I just have to work on getting this program to actually do what I want :-)

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    5
    Okay, one more question, I'm trying to get the program to open the file, delet everything up untill a certain point , and write the file I've already read in to the top of the new file (it is not a predetermined length, so it must insert, I want to leave the rest of the file intact). I can't seem to figure out how to insert the file with out overwriting. Thanks for all your help, I really appreciate it!

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you'll need a third file that you just copy each part into. Then delete and rename where appropriate.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    5
    Very good idea, I didn't even think of that. Thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 11:06 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 08:42 AM
  3. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 04:37 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 04:23 PM

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