Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String operations

String operations

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cpudood
    wrote on last edited by
    #1

    I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<

    M PJ ArendsP T 3 Replies Last reply
    0
    • C Cpudood

      I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Cpudood wrote: inData.open("data.dat"); Use the full path to the file. Without a path, the OS looks in the current directory, whatever that happens to be, and the file evidently isn't in that directory. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

      C 1 Reply Last reply
      0
      • M Michael Dunn

        Cpudood wrote: inData.open("data.dat"); Use the full path to the file. Without a path, the OS looks in the current directory, whatever that happens to be, and the file evidently isn't in that directory. --Mike-- Ericahist | Homepage | RightClick-Encrypt | 1ClickPicGrabber Latest art~!@#2rDFA#@(#*%$Rfa39f3fqwf--= NO CARRIER

        C Offline
        C Offline
        Cpudood
        wrote on last edited by
        #3

        I have inlcuded the data file as a part of the project workspace. I don't know if that is the current directory or not but I wouldn't know how to specify its location in the actual project.

        PJ ArendsP 1 Reply Last reply
        0
        • C Cpudood

          I have inlcuded the data file as a part of the project workspace. I don't know if that is the current directory or not but I wouldn't know how to specify its location in the actual project.

          PJ ArendsP Offline
          PJ ArendsP Offline
          PJ Arends
          wrote on last edited by
          #4

          If the data file is in the same folder as the app, then use GetModuleFileName() to get the app's path, lob off the app's name, and add the data file's name.


          [

          ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

          Within you lies the power for good; Use it!

          1 Reply Last reply
          0
          • C Cpudood

            I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            Cpudood wrote: getline(cin, line1); shouldn't that be

            getline(inData, line1);

            if you are reading in from the file? cin is reading in from the console.


            [

            ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            • C Cpudood

              I have tried this code segment on two different compilers receiving two different results. Using MSVC++6, the console reports that the file was not opened. C++.NET reports that the file was opened but none of the input data was displayed. What string operation do I need to use to output the contents of the file to the console? #include iostream #include fstream #include string using namespace std; string line1; string line2; string line3; string line4; ifstream inData; int main() { inData.open("data.dat"); if ( !inData) // Was file opened { cout<<"Could not open input file."; return 1; } else { cout<<"File was succesfully opened"<

              T Offline
              T Offline
              Terry ONolley
              wrote on last edited by
              #6

              2 things I can see: 1) Did you remap cin to your input stream somewhere in code that you didn't include? If not, your file could be open, but you aren't trying to read from it. 2) Where is your input file? if it is sitting in your project directory your code won't see it. You need to put the file in the Debug and Release directories - which is where your executable file is running from.



              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups