C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 16th, 2004, 11:40 AM
Iceman90 Iceman90 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Calgary, Alberta
Posts: 16 Iceman90 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old March 16th, 2004, 11:55 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
Not tested:

Code:
    for (int x=0; x<MAX; x++){
        file_ptr << numb[x] << endl;
    }
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #3  
Old March 16th, 2004, 03:37 PM
Iceman90 Iceman90 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: Calgary, Alberta
Posts: 16 Iceman90 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Anyone else?

Reply With Quote
  #4  
Old March 16th, 2004, 03:45 PM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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!

Reply With Quote
  #5  
Old March 16th, 2004, 04:03 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 7 m 41 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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.

Reply With Quote
  #6  
Old March 16th, 2004, 04:13 PM
nunki's Avatar
nunki nunki is offline
Checked Google ;)
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Phoenix, Arizona
Posts: 231 nunki User rank is Private First Class (20 - 50 Reputation Level)nunki User rank is Private First Class (20 - 50 Reputation Level) 
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

Reply With Quote
  #7  
Old March 16th, 2004, 05:46 PM
jubitzu jubitzu is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743 jubitzu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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])))))

Reply With Quote
  #8  
Old March 16th, 2004, 06:25 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 7 m 41 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
I was tired.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Writing An Array Of Numbers To A Text File

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap