|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I need code, who take some string of character, and to compare that a .txt file.
example: string = "qwe" .txt file = ''axcbhyqweklmnpouyxsqwelkopyqqwesd'' _____________((7)__________(20)_____(29) I need then program find in this text file a letters: ''qwe'' and call the number of that place, where this letter start in text. result must be: 7, 20, 29 please help me. ![]() |
| Sponsored Links |
|
|
|
|||||
|
If you are using C++, there are some string functions that can do this for you quite easily. If you are using C, it will be a bit trickier. Not knowing the language or what you've done so far, it's tough to help.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||||
|
I don't know which computer programming language you would prefer for a solution of such a problem. I hope that this source software code algorithm written in the computer programming language Assembly will fit your wishes.
Code:
.xlist
include stdlib.a
includelib stdlib.lib
.list
wp textequ <word ptr>
dseg segment para public 'data'
StrPtr dword ?
FileName dword ?
LineCnt dword ?
FVar filevar {}
InputLine byte 1024 dup (?)
dseg ends
cseg segment para public 'code'
assume cs:cseg
ds:dseg
ReadLn proc
push es
push ax
push di
push bx
lesi FVar
mov bx
0
ReadLp: fgetc
jc EndRead
cmp al
cr
je ReadLp
cmp al
lf
je EndRead
mov InputLine[bx]
al
inc bx
jmp ReadLp
EndRead: mov InputLine[bx]
0
pop bx
pop di
pop ax
pop es
ret
ReadLn endp
Main proc
mov ax
dseg
mov ds
ax
mov es
ax
meminit
argc
cmp cx
2
je GoodArgs
print
byte "Usage: Software_name 'string' filename"
cr
lf
0
jmp Quit
GoodArgs: mov ax
1
argv
mov wp StrPtr
di
mov wp StrPtr+2
es
mov ax
2
argv
mov wp Filename
di
mov wp Filename+2
es
mov ax
0
mov si
wp FileName
mov dx
wp FileName+2
lesi Fvar
fopen
jc BadOpen
mov wp LineCnt
0
mov wp LineCnt+2
0
SearchLp: call ReadLn
jc AtEOF
add wp LineCnt
1
adc wp LineCnt+2
0
lesi InputLine
mov dx
wp StrPtr+2
mov si
wp StrPtr
strstr
jc SearchLp
printf
byte "Found '%^s' at line %ld\n"
0
dword StrPtr
LineCnt
jmp SearchLp
AtEOF: lesi FVar
fclose
jmp Quit
BadOpen: printf
byte "Error attempting to open %^s\n"
cr
lf
0
dword FileName
Quit: ExitPgm
Main endp
cseg ends
sseg segment para stack 'stack'
stk db 1024 dup ("stack ")
sseg ends
zzzzzzseg segment para public 'zzzzzz'
LastBytes db 16 dup (?)
zzzzzzseg ends
end Main
__________________
Like an angel without a sense of mercy. Last edited by R-G; 04-15-2008 at 07:21 AM. |
|
|||||
|
If you mean to count all characters, you could probably just read each line, add them together into one string, and use length():
Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(int argc,char *argv[]){
string fileContents="",tempContents="";
ifstream readFile();
readFile.open("file_name.txt");
while(!readFile.eof()){
getline(readFile,tempContents);
fileContents+=tempContents;
}
cout<<"The amount of characters in the file should be: ";
cout<<fileContents.length();
cin.ignore();
return 0;
}
__________________
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tutorial: Starting C# with C# 2008 Express Edition | Jordan | CSharp Tutorials | 19 | 08-08-2008 01:48 PM |
| How to style fonts of a text in a simple page? | c0de | Tutorials | 3 | 09-15-2007 11:08 PM |
| Program to pass data from text file to table. | sania21 | Java Help | 3 | 05-28-2007 09:32 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |