#include <stdio.h>
#include <string.h>
struct n_rowdata
{
char m_name[27];
char m_enable[1];
char m_pointer[6];
char m_crlf[2];
};
constchar *myFile = "names.dat";
int startRecord = 101;
int endRecord = startRecord + 205;
int main()
{
int i;
FILE *f;
struct n_rowdata r;
f=fopen(myFile,"r+");
if (!f)
return 1;
for (i=startRecord;i<=endRecord;i++)
{
int sz = sizeof(struct n_rowdata);
fseek(f,sz*i,SEEK_SET);
fread(&r,sz,1,f);
printf("%s\%i",r.m_name,strlen(r.m_name));
}
fclose(f);
return 0;
}
With this code I read a part of text file in structure and print it to window. Adressing and getting data goes well but in line printf where should be printed member
r.m_name
it prints whole row and strlen prints length of whole row instead of one member of strucure.
What is incorrect here and how to get contents of member
r.m_name
or other member printed?
And second, for what reason is this line
struct n_rowdata r;
. Since structure
n_rowdata
is declared at top of program it should be public. Or not?