473,387 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

output from Files

Hi!!!!
I have some results in a results.txt file like this

#-50#-49#-48#-47#-46#-45#-44#-43#-42#-41#-40#-39#-38#-
-9#-8#-7#-6#-5#-4#-3#-2#-1#0#1#2#3#4#5#6#7#8#9#10#11#
12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#27#
97#98#99#100#101#102#103#104#105#106#107#108#109#

and i want to put these results in an array . How i could do that ?
Dec 30 '06 #1
3 1467
I wrote this piece of code :
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int ctr = 0;
  10.     int x;
  11.     int i = 0;
  12.     int array[161] ;
  13. //============================================================================== 
  14. //==============================================================================       
  15.     //prints on the screen  numbers from -50 to 109
  16.     for( x = -50 ; x < 110 ; x++ )
  17.     {
  18.        cout<< setw(3) << x << "#" << endl ;
  19.     }
  20.  
  21. //==============================================================================        
  22. //==============================================================================    
  23.     ofstream SaveFile("lala.txt" , ios::app );
  24.     //writes numbers from -50 to 109 in lala.txt
  25.     for( x = -50 ; x < 110 ; x++ )
  26.     {
  27.        SaveFile << setw(3) << x << "#" ;
  28.     }
  29.  
  30.     SaveFile.close();
  31.  
  32.     cout<< endl <<" =========" << " PHASE  1  " << "=========" << endl;
  33. //==============================================================================    
  34. //==============================================================================        
  35.     ifstream OpenFile("lala.txt");
  36.     static char str[4];
  37.     char ch ;
  38.  
  39.     while ( !OpenFile.eof() )
  40.     {
  41.        OpenFile.get(ch);
  42.        if ( ch == '#' )                               //when u meet ' # '
  43.        {
  44.            OpenFile.seekg(-3);                 //go 3 characters back
  45.            OpenFile >> str ;                     //and put the 3 characters in str
  46.            array[ctr] = atoi(str);                //convert str to int
  47.            OpenFile.seekg(4);                 //go 4 characters front so the inside -
  48.            ctr++ ;                                   //pointers shows on the next character 
  49.        }
  50. }
  51.     OpenFile.close();
  52.     cout<< endl <<" =========" << "END" << "=========" <<endl ;
  53.  
  54.  
  55.     return 0;
  56.  
  57. }
  58.  
  59.  
The problem is at this block :
Expand|Select|Wrap|Line Numbers
  1. {
  2.            OpenFile.seekg(-3);                 //go 3 characters back
  3.            OpenFile >> str ;                     //and put the 3 characters in str
  4.            array[ctr] = atoi(str);                //convert str to int
  5.            OpenFile.seekg(4);                 //go 4 characters front so the inside -
  6.            ctr++ ;                                   //pointers shows on the next character 
  7. }
  8.  
  9.  
Can you find what i do wrong ??????
Dec 30 '06 #2
Also inside the lala.txt there is :

-50#-49#-48#-47#-46#-45#-44#-43#-42#-41#-40#-39#-38#-37#-36#-35#-34#-33#-32#-31#-30#-29#-28#-27#-26#-25#-24#-23#-22#-21#-20#-19#-18#-17#-16#-15#-14#-13#-12#-11#-10# -9# -8# -7# -6# -5# -4# -3# -2# -1# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# 11# 12# 13# 14# 15# 16# 17# 18# 19# 20# 21# 22# 23# 24# 25# 26# 27# 28# 29# 30# 31# 32# 33# 34# 35# 36# 37# 38# 39# 40# 41# 42# 43# 44# 45# 46# 47# 48# 49# 50# 51# 52# 53# 54# 55# 56# 57# 58# 59# 60# 61# 62# 63# 64# 65# 66# 67# 68# 69# 70# 71# 72# 73# 74# 75# 76# 77# 78# 79# 80# 81# 82# 83# 84# 85# 86# 87# 88# 89# 90# 91# 92# 93# 94# 95# 96# 97# 98# 99#100#101#102#103#104#105#106#107#108#109#
Dec 30 '06 #3
horace1
1,510 Expert 1GB
the reading of the data is very complex - when reading you can simply read an int then a char, e.g.
Expand|Select|Wrap|Line Numbers
  1.     ifstream OpenFile("lala.txt");
  2.     if ( OpenFile)
  3.     {
  4.        while(OpenFile >> array[ctr])   // read an int
  5.           {  ctr++; OpenFile.get(); }     // read the #
  6.     }
  7.  
when you are creating the data you are appending to any existing file so it is getting longer and longer and will overrun the end of your array when reading
Dec 30 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Isaac Councill | last post by:
Hello, This seems like a newbie question, but I couldn't find the answer on google. I've been using xsl to transform rdf files into runnable programs in another (non-markup) language. It's...
0
by: Dimitre Novatchev | last post by:
You seem to be unaware of the xslt processing which uses the built-in rules in the absence of templates that match some selected node. http://www.w3.org/TR/xslt#built-in-rule According to the...
3
by: Jorge Cecílio | last post by:
Hi! I would like to export some MS-Access reports output to pdf. However, the only possibility offered by Access (afaik) for me to export formatted output is snp (snapshot) (I use MS-Office...
0
by: IKdev | last post by:
Hi all, I have win2003 server and NET 1.1 framework. Created simple ASP .NET app with one page that has button and a label. When trying to run got the CS0016 error (see below). My temp env....
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
3
by: jonathan184 | last post by:
script is printing output correct but not the actual output. Basically what the script is doing it taking a 1 flat file then it is splits the file into smaller files in 1000 record increments ...
3
by: undshan | last post by:
I am writing a code that needs to open a file, create an output file, run through my function, prints the results to the output file, and closes them within a loop. Here is my code: #include...
12
by: vj | last post by:
I am using Borland C++. I run a program which generates output data files, say 'Voltage.dat', 'Current.dat'. I have a variable in my code (say N), and for various values of N (for each value of...
1
by: lqnt1981 | last post by:
Following is the code in python that opens all files under a directories and puts a comma in place of whitespace, I can see the output on the command line, but I am not sure what to do in order to...
1
by: Jesse Aldridge | last post by:
I want to put all the output from all of my python programs in one place. I've been trying to get this working for the last few days, but there are lots of annoying little details that are making...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.