Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 17, 2006, 08:13 PM   #1
robert17
macrumors newbie
 
Join Date: Jul 2006
Working with Files in C++ Question

I am learning C++ and have encountered troubles trying to read data from a file into my program. The program is meant to read an integer value from a text file and print it to the screen. It prints a number to the screen, but it is definitely not the one from the file (12 v. -1880995964). I using XCode version 2.3 with Mac OS 10.4.6. I have tried various methods, but assume that I am creating a new file, selecting "Empty File in Project" and naming it "input.txt". My code is as follows:

Code:

#include <iostream>
#include <fstream>

using namespace std;

int main () 
{

     int num;
     ifstream inData;

     inData.open("input.txt");

     inData >> num;

     cout << num;

    return 0;
}
There is nothing in the text file except for the number "12". I appreciate any help that you can give!

Robert
robert17 is offline   0 Reply With Quote
Old Jul 17, 2006, 08:21 PM   #2
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
is there a newline char after the 12?

there probably should be.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Jul 17, 2006, 08:31 PM   #3
robert17
Thread Starter
macrumors newbie
 
Join Date: Jul 2006
I tried adding the '\n' after the 12, but it still gave me "-1880995964". Thanks.
robert17 is offline   0 Reply With Quote
Old Jul 17, 2006, 08:34 PM   #4
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Quote:
Originally Posted by robert17
I tried adding the '\n' after the 12, but it still gave me "-1880995964". Thanks.
Manually adding \n won't do it. What I meant was, did you hit return after the 12? That'll write the newline char to the file (which C++ will read properly).

I haven't used C++ in a while (nor do I have gcc installed at the moment), so I'm not much help. Sorry.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Jul 17, 2006, 08:56 PM   #5
robert17
Thread Starter
macrumors newbie
 
Join Date: Jul 2006
Ah, ok. I went ahead and tried that too, but it gave the same results. Thanks for the try.
robert17 is offline   0 Reply With Quote
Old Jul 18, 2006, 01:30 PM   #6
szymczyk
macrumors regular
 
Join Date: Mar 2006
You should initialize the variable num to 0 (or some other value) before reading the file. If you run your program and it returns 0 instead of 12, you have a problem reading the file.

I suspect your problem is that the program can't find the file input.txt. The program looks in the working directory for the file, and if the file is not in that directory, the program won't be able to open the file. Xcode initially sets the working directory to your Build Products directory which is the following directory:

ProjectName/build/BuildConfiguration

BuildConfiguration will be either Debug or Release. Either move the input.txt file to the Build Products directory or specify the full path to the file in your code instead of just the name input.txt.
__________________
Mark Szymczyk
Author, Xcode Tools Sensei
http://www.meandmark.com
szymczyk is offline   0 Reply With Quote
Old Jul 18, 2006, 04:24 PM   #7
SamMiller0
macrumors member
 
Join Date: Aug 2004
Location: San Jose, CA
use the ifstream::good() method to check if you opened the file successfully before attempting to read from it
SamMiller0 is offline   0 Reply With Quote
Old Jul 18, 2006, 05:03 PM   #8
robert17
Thread Starter
macrumors newbie
 
Join Date: Jul 2006
Much thanks Mark. I initialized num and it came back as zero so I moved the file and it worked properly. I appreciate the help!
robert17 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Searching for System Files not Working? flatline82 OS X Mavericks (10.9) 15 Jul 20, 2014 03:52 PM
Show hidden files not working in Mavericks BlueGummiBear OS X Mavericks (10.9) 40 Jun 24, 2014 09:10 PM
Help needed! Copying files via terminal not working (error -36) jamierorison OS X 2 May 17, 2013 01:06 PM
Working Bat Files for Gaming on the rMBP Newfiejudd MacBook Pro 0 Mar 15, 2013 02:26 PM
My files are gone in my external harddrive; It was working before and I have NTFS-3G. Zyrl Mac Basics and Help 1 Feb 8, 2013 12:28 PM

Forum Jump

All times are GMT -5. The time now is 01:56 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC