The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Writing An Array Of Numbers To A Text File
Discuss Writing An Array Of Numbers To A Text File in the C Programming forum on Dev Shed. Writing An Array Of Numbers To A Text File C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 16th, 2004, 11:40 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Location: Calgary, Alberta
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Writing An Array Of Numbers To A Text File
Hello all.
I am writing a program and need some help.
I have an array of numbers, ( int numbs[MAX] ) with MAX being a pre-determined maximum value.
How would I go about writing this array worth of numbers to a file? I can write from file to array, but can't seem to make it work going backwards.
If anyone can post some code to help me out, it would be appreciated.
thanks.
P.S.
Here is my code so far. Some of the code is commented out because I was trying different things. This is sloppy, but gives an idea of what I need and where I am at.
Code:
void write_final_file()
{
system ("cls");
int numbs[MAX]; // the array of numbers
ofstream file_ptr ("sorted.txt", ios::out | ios::trunc); //creates the file to be written to.
//Error Trapping
if (!file_ptr) //If The File Can't Be Opened
{
cout<<"**This File Has Been Abducted By Aliens!**"<<endl;
cout<<"**Error Opening File**"<<endl; //Error Message
cout<<"**Program Will Be Terminated**"<<endl;
system ( "PAUSE" );
exit(1); //Terminates the program
}
//End Of Error Trapping
//for (int x=0; x<MAX; x++)
//{
// NEED THIS CODE!!
//writes the numbers from the array to the file
//}
file_ptr.close(); //closes the file
cout<<"Your Numbers Have Been Written To The File Sorted.txt"<<endl;
cout<<"Press Any Key To Return To The Main Menu"<<endl;
_getch();
}
Last edited by Iceman90 : March 16th, 2004 at 11:45 AM.
Reason: Fixed The Code
|

March 16th, 2004, 11:55 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
Not tested:
Code:
for (int x=0; x<MAX; x++){
file_ptr << numb[x] << endl;
}
|

March 16th, 2004, 03:37 PM
|
|
Registered User
|
|
Join Date: Feb 2004
Location: Calgary, Alberta
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Anyone else?
|

March 16th, 2004, 03:45 PM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
So did you try mine and it didn't work? Just because I didn't compile and run it doesn't mean I don't know what I am talking about!
|

March 16th, 2004, 04:03 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
We didn't realize it was a poll.
Not tested. Thanks to Mitakeet for all but four characters.
Code:
for (int x=0; x<MAX; x++){
file_ptr << *(&numb[x]) << endl;
}
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
|

March 16th, 2004, 04:13 PM
|
 |
Checked Google ;)
|
|
Join Date: Dec 2003
Location: Phoenix, Arizona
Posts: 231

Time spent in forums: 2 Days 1 h 11 m 6 sec
Reputation Power: 10
|
|
Mitakeets method worked fine for me 
__________________
No, I'm from Iowa. I just work in outer space.
-- James T. Kirk
|

March 16th, 2004, 05:46 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743
Time spent in forums: 21 h 16 m 10 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
|
i like dawei's method because it references then dereferences the memory: why not *(&(*(&(*(&(numb[x])))))
|

March 16th, 2004, 06:25 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
I was tired. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|