ifstream

I

idikoko

I am writing an absolute assembler; I am trying to read a file :
.model tiny
.code
org 100H
bgind: mov ax,alist ; address of list
mov bx,ax ; put in bx
mov ax,five ; number of numbers
mov cx,ax ; copy to cx
sub dx,dx ; clear dx
again: mov ax,[bx] ; get a number
add dx,ax ; add to dx
mov ax,one
sub cx,ax ; decrement cx
mov ax,two
add bx,a, ; point to next
mov ax,zero
comp cs,ax ; are we done?
jne again ; no
mov ax,dx
mov sum,ax ; store result
jmp around
alist dw list
sum dw 0
five dw 5
zero dw 0
one dw 1
two dw 2
list dw 45
dw -7
dw 100
dw 44
dw -25
exit dw 4c00H
around: mov ax,exit
dw 021CDh
end begin


line by line and break up each line using a tokenizer from from the
<string> lib however I'm not sure what instruction in the ostream lib
to use to extract line by line from the file. I wanna consider spaces
too. Help anyone?
 
S

Sumit Rajan

idikoko said:
I am writing an absolute assembler; I am trying to read a file :
.model tiny
.code
org 100H
bgind: mov ax,alist ; address of list
mov bx,ax ; put in bx
mov ax,five ; number of numbers
mov cx,ax ; copy to cx
sub dx,dx ; clear dx
again: mov ax,[bx] ; get a number
add dx,ax ; add to dx
mov ax,one
sub cx,ax ; decrement cx
mov ax,two
add bx,a, ; point to next
mov ax,zero
comp cs,ax ; are we done?
jne again ; no
mov ax,dx
mov sum,ax ; store result
jmp around
alist dw list
sum dw 0
five dw 5
zero dw 0
one dw 1
two dw 2
list dw 45
dw -7
dw 100
dw 44
dw -25
exit dw 4c00H
around: mov ax,exit
dw 021CDh
end begin


line by line and break up each line using a tokenizer from from the
<string> lib however I'm not sure what instruction in the ostream lib
to use to extract line by line from the file. I wanna consider spaces
too. Help anyone?

#include <fstream>
#include <string>

int main()
{
std::string line;
std::ifstream ifs("filename.txt");
while (getline(ifs, line)) {
// do what you have to do with each line
}
}

Regards,
Sumit
 
E

Eric Pruneau

Sumit Rajan said:
idikoko said:
I am writing an absolute assembler; I am trying to read a file :
.model tiny
.code
org 100H
bgind: mov ax,alist ; address of list
mov bx,ax ; put in bx
mov ax,five ; number of numbers
mov cx,ax ; copy to cx
sub dx,dx ; clear dx
again: mov ax,[bx] ; get a number
add dx,ax ; add to dx
mov ax,one
sub cx,ax ; decrement cx
mov ax,two
add bx,a, ; point to next
mov ax,zero
comp cs,ax ; are we done?
jne again ; no
mov ax,dx
mov sum,ax ; store result
jmp around
alist dw list
sum dw 0
five dw 5
zero dw 0
one dw 1
two dw 2
list dw 45
dw -7
dw 100
dw 44
dw -25
exit dw 4c00H
around: mov ax,exit
dw 021CDh
end begin


line by line and break up each line using a tokenizer from from the
<string> lib however I'm not sure what instruction in the ostream lib
to use to extract line by line from the file. I wanna consider spaces
too. Help anyone?

#include <fstream>
#include <string>

int main()
{
std::string line;
std::ifstream ifs("filename.txt");
while (getline(ifs, line)) {
// do what you have to do with each line
}
}

Regards,
Sumit

I'll add this

#include <fstream>
#include <string>
#include <sstream>

int main()
{
std::string line;
std::ifstream ifs("filename.txt");
while (getline(ifs, line)) {
std::istringstream iss(line);
std::string word;
while(iss >> word)
{
// do what you have to do with each word in the line
// beware, instruction like dx,bx will be taken as 1 word.
// the >> operator will extract every words separated by one or
more space (or tab)
}
}
}


Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top