Read/edit/write from/to files

G

Guest

Hello everbody,
I know C++, so below is a code I wrote that opens/reads a file and looks for
certain character and writes into another file all X and Y coordinates of a
pecific character.

I would like to build a matrix using VB:
I need help on how to open and read a text file(read every character in
file), then create a matrix and insert everyone of those characters into a
table (matrix) in VB.

Could anybody help with this.
int main()
{
FILE *infile, *InkLocsFile;
char line[MAXLINE]={0}; //wmapBitLoc[rows][cols]
int n=0,i=0,j=0, flag=0, cl=0,Ink=0, rw = -1;

infile = fopen("wmap.txt", "r"); //open fail file for reading and writing
if (infile == NULL)
{
printf("\nError (Error opening wmap file): %s file failed to
open.\n",infile);
exit(0);
}//end if


InkLocsFile = fopen("InkLocsFile.txt", "w");
fprintf (InkLocsFile, "RowLoc ColLoc \n");
fprintf (InkLocsFile, "----- ------ \n");

while ( (fgets (line,sizeof(line),infile) != NULL))
{
rw +=1;
// if (strstr(line))
// {
for (n=0; n<MAXLINE; n++)
{
if (line[n] == 48)
{
cl =n;
fprintf (InkLocsFile, "%2d %10d \n",rw, cl);
}//end if (line[n] == 0)

}// end for

// }//end if strstr()

}//end While
fclose (infile);
fclose (InkLocsFile);
return 0;
}//end main()
C++ code:
 
G

Guest

Hi,
I got an answer to a big part of my question the following vb code which
reads a file, line by line and outputs line into a message box.
How can I access single characters in this array line. in c++ I would do
line[0] and access the first char and so on.
Could anybody help?

Private Sub Command1_Click()
Dim lngIndex As Long
Dim intFile As Integer
'file identification number
Dim strFile As String
'filename
Dim strData As String
'string to contain complete file
Dim strLine() As String
'array to contain each line
Dim intChoice As Integer
'user selection in msgbox
intFile = FreeFile
'unique file identification number
strFile = "c:\temp\myfile.txt"
'filename to read
Open strFile For Input As #intFile
'open file to read
strData = Input(Lof(intFile), #intFile)
'read in complete file
Close #intFile
'close file
strLine = Split(strData, vbCrLf)
'split total data into array of line
For lngIndex = 0 To UBound(strLine)
'loop through all lines (array)
intChoice = MsgBox(strLine(lngIndex), vbOKCancel, "Line: " &
CStr(lngIndex)) 'show each line
If intChoice = vbCancel Then Exit For
'select 'cancel' to jump out of loop
Next lngIndex
End sub
 
D

Dave Peterson

I think you want to take a look at mid(). You may want to review left(),
right() and other string functions, too.

dim cCtr as long

For lngIndex = 0 To UBound(strLine)
for cctr = 1 to len(strline(lngindex))
if mid(strline(lngindex),cctr,1) = "A" then
msgbox "Found an Upper A"
end if
next cCtr
......
Next lngIndex
Hi,
I got an answer to a big part of my question the following vb code which
reads a file, line by line and outputs line into a message box.
How can I access single characters in this array line. in c++ I would do
line[0] and access the first char and so on.
Could anybody help?

Private Sub Command1_Click()
Dim lngIndex As Long
Dim intFile As Integer
'file identification number
Dim strFile As String
'filename
Dim strData As String
'string to contain complete file
Dim strLine() As String
'array to contain each line
Dim intChoice As Integer
'user selection in msgbox
intFile = FreeFile
'unique file identification number
strFile = "c:\temp\myfile.txt"
'filename to read
Open strFile For Input As #intFile
'open file to read
strData = Input(Lof(intFile), #intFile)
'read in complete file
Close #intFile
'close file
strLine = Split(strData, vbCrLf)
'split total data into array of line
For lngIndex = 0 To UBound(strLine)
'loop through all lines (array)
intChoice = MsgBox(strLine(lngIndex), vbOKCancel, "Line: " &
CStr(lngIndex)) 'show each line
If intChoice = vbCancel Then Exit For
'select 'cancel' to jump out of loop
Next lngIndex
End sub

David said:
Hello everbody,
I know C++, so below is a code I wrote that opens/reads a file and looks for
certain character and writes into another file all X and Y coordinates of a
pecific character.

I would like to build a matrix using VB:
I need help on how to open and read a text file(read every character in
file), then create a matrix and insert everyone of those characters into a
table (matrix) in VB.

Could anybody help with this.
int main()
{
FILE *infile, *InkLocsFile;
char line[MAXLINE]={0}; //wmapBitLoc[rows][cols]
int n=0,i=0,j=0, flag=0, cl=0,Ink=0, rw = -1;

infile = fopen("wmap.txt", "r"); //open fail file for reading and writing
if (infile == NULL)
{
printf("\nError (Error opening wmap file): %s file failed to
open.\n",infile);
exit(0);
}//end if


InkLocsFile = fopen("InkLocsFile.txt", "w");
fprintf (InkLocsFile, "RowLoc ColLoc \n");
fprintf (InkLocsFile, "----- ------ \n");

while ( (fgets (line,sizeof(line),infile) != NULL))
{
rw +=1;
// if (strstr(line))
// {
for (n=0; n<MAXLINE; n++)
{
if (line[n] == 48)
{
cl =n;
fprintf (InkLocsFile, "%2d %10d \n",rw, cl);
}//end if (line[n] == 0)

}// end for

// }//end if strstr()

}//end While
fclose (infile);
fclose (InkLocsFile);
return 0;
}//end main()
C++ code:
 
G

Guest

Thanks dave,
This helped me go one step further.
This is the code I have so far.
I am reading characters from file, bulding a matrix and I am trying to
insert the char I am reading into the matrix (cells). Can you help with this
syntax
--------------------------------------------------
grille2.Cell.... = X ' This is the syntax I need help with
???
' Trying to insert char into all
cells
--------------------------------------


Private Sub Command1_Click()
Dim lngIndex As Long
Dim intFile As Integer
'file identification number
Dim strFile As String
'filename
Dim strData As String
'string to contain complete file
Dim strLine() As String
'array to contain each line
Dim intChoice As Integer
'user selection in msgbox
Dim cCtr As Long

grille2.Height = 6000
grille2.Width = 6000
Dim NumbCol As Integer
NumbCol = grille2.Height \ 20 ' set nbre of cols
Dim NumbRow As Integer
NumbRow = grille2.Height \ 20 ' set nbre of rows
'unique file
identification number
strFile = "c:\wmap.txt"
'filename to read
grille2.Cols = NumbCol
grille2.Rows = NumbRow
For i = 0 To grille2.Cols - 1
grille2.ColWidth(i) = grille2.Width / 100 ' Set the width of each col
Next i
For j = 0 To grille2.Rows - 1
grille2.RowHeight(j) = grille2.Height / 100 ' Set the height of each
row
Next j
intFile = FreeFile
strFile = "c:\wmap.txt"
Open strFile For Input As #intFile
'open file to read
strData = Input(LOF(intFile), #intFile)
'read in complete file
Close #intFile
'close file
strLine = Split(strData, vbCrLf)
'split total data into array of line
For lngIndex = 0 To UBound(strLine)
'loop through all lines (array)
For cCtr = 1 To Len(strLine(lngIndex))
' If Mid(strLine(lngIndex), cCtr, 1) = "X" Then
Mid(strLine(lngIndex), cCtr, 1) = "X"
grille2.Row = lngIndex
grille2.Col = cCtr
-------------------------------------
grille2.Cell.... = X ' This is the syntax I need help with
grille2.CellBackColor = vbRed ' I will insert other if
statments
--------------------------------------
' intChoice = MsgBox(strLine(lngIndex), vbOKCancel, "Line: "
& CStr(lngIndex)) 'show each line
' MsgBox "Found an Upper X"
' End If
Next cCtr

' intChoice = MsgBox(strLine(lngIndex), vbOKCancel, "Line: " &
CStr(lngIndex)) 'show each line
If intChoice = vbCancel Then Exit For
'select 'cancel' to jump out of loop
Next lngIndex

End Sub

Dave Peterson said:
I think you want to take a look at mid(). You may want to review left(),
right() and other string functions, too.

dim cCtr as long

For lngIndex = 0 To UBound(strLine)
for cctr = 1 to len(strline(lngindex))
if mid(strline(lngindex),cctr,1) = "A" then
msgbox "Found an Upper A"
end if
next cCtr
......
Next lngIndex
Hi,
I got an answer to a big part of my question the following vb code which
reads a file, line by line and outputs line into a message box.
How can I access single characters in this array line. in c++ I would do
line[0] and access the first char and so on.
Could anybody help?

Private Sub Command1_Click()
Dim lngIndex As Long
Dim intFile As Integer
'file identification number
Dim strFile As String
'filename
Dim strData As String
'string to contain complete file
Dim strLine() As String
'array to contain each line
Dim intChoice As Integer
'user selection in msgbox
intFile = FreeFile
'unique file identification number
strFile = "c:\temp\myfile.txt"
'filename to read
Open strFile For Input As #intFile
'open file to read
strData = Input(Lof(intFile), #intFile)
'read in complete file
Close #intFile
'close file
strLine = Split(strData, vbCrLf)
'split total data into array of line
For lngIndex = 0 To UBound(strLine)
'loop through all lines (array)
intChoice = MsgBox(strLine(lngIndex), vbOKCancel, "Line: " &
CStr(lngIndex)) 'show each line
If intChoice = vbCancel Then Exit For
'select 'cancel' to jump out of loop
Next lngIndex
End sub

David said:
Hello everbody,
I know C++, so below is a code I wrote that opens/reads a file and looks for
certain character and writes into another file all X and Y coordinates of a
pecific character.

I would like to build a matrix using VB:
I need help on how to open and read a text file(read every character in
file), then create a matrix and insert everyone of those characters into a
table (matrix) in VB.

Could anybody help with this.
int main()
{
FILE *infile, *InkLocsFile;
char line[MAXLINE]={0}; //wmapBitLoc[rows][cols]
int n=0,i=0,j=0, flag=0, cl=0,Ink=0, rw = -1;

infile = fopen("wmap.txt", "r"); //open fail file for reading and writing
if (infile == NULL)
{
printf("\nError (Error opening wmap file): %s file failed to
open.\n",infile);
exit(0);
}//end if


InkLocsFile = fopen("InkLocsFile.txt", "w");
fprintf (InkLocsFile, "RowLoc ColLoc \n");
fprintf (InkLocsFile, "----- ------ \n");

while ( (fgets (line,sizeof(line),infile) != NULL))
{
rw +=1;
// if (strstr(line))
// {
for (n=0; n<MAXLINE; n++)
{
if (line[n] == 48)
{
cl =n;
fprintf (InkLocsFile, "%2d %10d \n",rw, cl);
}//end if (line[n] == 0)

}// end for

// }//end if strstr()

}//end While
fclose (infile);
fclose (InkLocsFile);
return 0;
}//end main()
C++ code:
 

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

Top