cstring

This is a discussion on cstring within the C++ Programming forums, part of the General Programming Boards category; I am trying to firstly to open an existing .txt file to display in my output. i have created a ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    cstring

    I am trying to firstly to open an existing .txt file to display in my output. i have created a normal notepad with a few lines of txt that i would like to view. i have been trying to use fstream but having no luck. i keep getting that #include <fstream.h> does not exist. using #include <fstream> i just don't get it too open.

    i am then trying to assign each word in that string to an array and then join the array after to eliminate all the double and triple spaces in the string. i am not having an luck and starting to get really frustrated. can someone help please.!!!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    what do you mean by
    i just don't get it too open.
    if you program in C - use stdio.h not fstream

    if youo want to use c++ use also c++ strings
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    well i can write text to a file and then open it to read. i can only do this in 2 seperate programs though. the main problem i am having is that i have bee given a notepad .txt document that i need to open and then edit the txt in that document. editing text is not too difficult but i can never seem to open that file.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    
    int main ()
    {
    	char buffer [256];
    	ifstream examplefile ("example.txt");
    	if (! examplefile.is_open())
    	{
    		cout<<"error opening file";
    		exit (1);
    	}
    	while (! examplefile.eof())
    	{
    		examplefile.getline (buffer,100);
    		cout<<buffer<<endl;
    	}
    	return 0;
    }
    this is the code i am trying to use but i always get "error opening file".

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Moved to C++, now that we've established that it is actually C++ file io you're working with.

    ***

    [edit]Check the file is in the application/project directory from which you run the project - test it by giving the full path to the file name.

    Also, prefer std::string over c-style strings and use std::getline to read the contents of a text file one line at a time, eg:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main ()
    {
      std::string line;
      
      std::ifstream file("example.txt");
      
      if (!file)
        {
        std::cerr<<"Can't open file"<<std::endl;
        return EXIT_FAILURE;
        }
     //read file one line at a time
      while (std::getline(file,line))
        {
        //do stuff with line
        std::cout<<line<<'\n';
        }
      std::cout<<std::endl;
    }
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8
    that worked. great. thank you very much. now i can try and carry on with the rest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Writing CObjects with Serialization to CArchive
    By ruben_gerad in forum C++ Programming
    Replies: 0
    Last Post: 11-09-2006, 07:25 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Help with CString class and operator+ overloading
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 10:23 AM
  5. Operater overloading - (const char + CString)
    By auth in forum C++ Programming
    Replies: 14
    Last Post: 10-24-2002, 09:22 AM

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