Whats up guys. I am new to programming and just started a week ago. I am doing this just for fun. I have a problem with a text file that i am trying to read in. I am able to read in the text file, i just need to know how to elminate some lines from the text file. So, this is what i am trying to do

example Text file ;

'Memory Loader
'DAte
'Time
000001
000002
000003
000004
000005
000006
000000
000000
'variables
012012
012021
020102
'variables
001201

What i am trying to do is read in this file and place only lines that have numbers "000001" into an array. i have set up two arrays to put the numbers in. the first 8 lines of "numbers" go into one array and the remaining numbers go into another array. My problem is i do not know how to ignore the lines of text ex. " 'variables " and so forth. Here is what i have so far and thank you very much for any help you can give thanks

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
    ifstream in ("C:\\Documents and Settings\\HP_Administrator\\Desktop\\test.txt");
    float registers[7];
    int memory[4000];
    int i;
    int k;
    int j;
    
    for (i=0; i<8; i++)
    
    in >> registers[i];
    
    for (k=0; k<18; k++)
    
    in >> memory[k];
    
    cout << "Registers" << endl;
    cout << "Register 1 " << registers[0] << endl;
    cout << "Register 1 " << registers[1] << endl;
    cout << "Register 1 " << registers[2] << endl;
    cout << "Register 1 " << registers[3] << endl;
    cout << "Register 1 " << registers[4] << endl;
    cout << "Register 1 " << registers[5] << endl;
    cout << "SP         " << registers[6] << endl;
    cout << "PC         " << registers[7] << endl;
    
    cout << "Memory Dump" << endl;
    
    for (j=0; j<18; j++)
    
    cout << memory[j] << endl;
    
    in.close(); 
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 10 Replies

Your code and your input file don't match. I cannot tell which things you are trying to put where.

Please try to explain what goes where and I will try to help.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{

// Opens the Text File in is my "cin" in essence
ifstream in ("C:\\Documents and Settings\\HP_Administrator\\Desktop\\test.txt");

//both arrays in which i will place the text from the text file. "registers" will get the first 8 lines of numbers and memory will get the remaining lines of numbers
int registers[7];
int memory[4000];
int i;
int k;
int j;


//reads in the lines and places them into the arrays

for (i=0; i<8; i++)

in >> registers;

for (k=0; k<18; k++)

in >> memory[k];

//dislays the contents of the arrays on the screen
cout << "Registers" << endl;
cout << "Register 1 " << registers[0] << endl;
cout << "Register 1 " << registers[1] << endl;
cout << "Register 1 " << registers[2] << endl;
cout << "Register 1 " << registers[3] << endl;
cout << "Register 1 " << registers[4] << endl;
cout << "Register 1 " << registers[5] << endl;
cout << "SP " << registers[6] << endl;
cout << "PC " << registers[7] << endl;

cout << "Memory Dump" << endl;

for (j=0; j<18; j++)

cout << memory[j] << endl;

in.close();

system("PAUSE");
return EXIT_SUCCESS;
}

Eh, OK.

When people ask for clarification, it is not usually very helpful to re-post what you already said, as if we're stupid.

I will assume by the added comment

both arrays in which i will place the text from the text file. "registers" will get the first 8 lines of numbers and memory will get the remaining lines of numbers

that you mean you want to skip everything in the file until you find a number, because the file has no structure.

I also am going to assume that each line in the file contains a single value.

Your best bet, then, is to get each line, then parse it to see if it is a number. If it is, store the number. If not, keep going.

...
#include <sstream>
...
...
int registers[ 7 ];
int memory[ 4000 ];
int i;
string s;

// read registers until eight integers are read
i = 0;
while (getline( in, s )) {
  if (stringstream( s ) >> registers[ i ]) i++;
  if (i == 8) break;
  }

// read memory until the end of file
i = 0;
while (getline( in, s )) {
  if (stringstream( s ) >> memory[ i ]) i++;
  }
in.close();
...

I hope this helps.

I will assume by the added comment...
I also am going to assume ...

I wouldn't. I've wasted my time making assumptions... :icon_wink:

Since you don't want to explain what's going on, just start putting output statements in your code to see if the values are correct as the program executes.

And Read This! before you post again.

Thank You Very Much!! Works Great!!

One more question: Like i said im new to this so bear with me. but my output works like i want it too except it does not display zeroes

ex.
my output is
1
2
3
4
5
6

where as i need it to display the preceeding zeroes

ex.

000001
000002
000003
000004
000005
000006

You need to #include <iomanip> and add setw() and setfill() to your output statements.

Something for James Bond: cout << setfill( '0' ) << setw( 3 ) << 7; Hope this helps.

[EDIT] Oh yeah, if you think your numbers might be negative, use the internal flag: cout << setfill( '0' ) << internal << setw( 4 ) << -42;

You Are The Man!! Thanks Again

curious question... how would i convert a binary number to hexidecimal.... im sure there is a library that already does it for you ?

Depends on what your definition of binary number is. A string with all 1's and 0's? An integer value containing only 1's and 0's? Or an integer that you can display as binary?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.