structure to file [REMAIN UNSOLVED] [CLOSED]
-
#include #include using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail st;
DWORD code;
};int main()
{
Student st;}
how to write and read structure to a file using WriteFile() and ReadFile().:confused:
Some Day I Will Prove MySelf :: GOLD
modified on Saturday, February 19, 2011 9:43 AM
-
#include #include using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail st;
DWORD code;
};int main()
{
Student st;}
how to write and read structure to a file using WriteFile() and ReadFile().:confused:
Some Day I Will Prove MySelf :: GOLD
modified on Saturday, February 19, 2011 9:43 AM
-
#include #include using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail st;
DWORD code;
};int main()
{
Student st;}
how to write and read structure to a file using WriteFile() and ReadFile().:confused:
Some Day I Will Prove MySelf :: GOLD
modified on Saturday, February 19, 2011 9:43 AM
-
Student st; WriteFile(file,&st,sizeof(st),&written,0); open file using Createfile API, or you can use fopen("file","wb"); and fwrite C run time APIS. is it so complicated? :)
If u can Dream... U can do it
jk chan wrote:
you can use fopen("file","wb"); and fwrite C run time APIS. is it so complicated?
i know how to read and write using fread and fwrite. But i want my program to use WriteFile() and ReadFile()
Some Day I Will Prove MySelf :: GOLD
-
Something like
DWORD dwWritten;
BOOL result = WriteFile(hFile, &st, sizeof st, &dwWritten, NULL);Use a similar form to read it back.
I must get a clever new signature for 2011.
#include <windows.h>
#include <conio.h>
#include <iostream>using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};int main()
{DWORD BytesWritten=0, BytesRead=0; Student st,read; st.code =1; st.detail.Name = TEXT("Williams"); st.detail.Age = 25; st.detail.Address = TEXT("B-33 Lane 5"); HANDLE hFile = CreateFile(TEXT("C:\\\\demo.txt"),GENERIC\_READ | GENERIC\_WRITE,0,0,OPEN\_ALWAYS,FILE\_ATTRIBUTE\_NORMAL,0); if (hFile != INVALID\_HANDLE\_VALUE) { WriteFile(hFile,&st,sizeof(Student),&BytesWritten,0); ReadFile(hFile,&read,sizeof(Student),&BytesRead,0); CloseHandle(hFile); cout<<read.code<<endl; cout<<read.detail.Name; } else { cout<<"Error! Cannot Open File"; exit(1); } \_getche(); return (0);
}
when executing this code following error occurs.
Error 1 error C2440: '=' : cannot convert from 'const wchar_t [9]' to 'wchar_t [25]'
Error 2 error C2440: '=' : cannot convert from 'const wchar_t [12]' to 'wchar_t [100]'Some Day I Will Prove MySelf :: GOLD
-
#include <windows.h>
#include <conio.h>
#include <iostream>using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};int main()
{DWORD BytesWritten=0, BytesRead=0; Student st,read; st.code =1; st.detail.Name = TEXT("Williams"); st.detail.Age = 25; st.detail.Address = TEXT("B-33 Lane 5"); HANDLE hFile = CreateFile(TEXT("C:\\\\demo.txt"),GENERIC\_READ | GENERIC\_WRITE,0,0,OPEN\_ALWAYS,FILE\_ATTRIBUTE\_NORMAL,0); if (hFile != INVALID\_HANDLE\_VALUE) { WriteFile(hFile,&st,sizeof(Student),&BytesWritten,0); ReadFile(hFile,&read,sizeof(Student),&BytesRead,0); CloseHandle(hFile); cout<<read.code<<endl; cout<<read.detail.Name; } else { cout<<"Error! Cannot Open File"; exit(1); } \_getche(); return (0);
}
when executing this code following error occurs.
Error 1 error C2440: '=' : cannot convert from 'const wchar_t [9]' to 'wchar_t [25]'
Error 2 error C2440: '=' : cannot convert from 'const wchar_t [12]' to 'wchar_t [100]'Some Day I Will Prove MySelf :: GOLD
goldenrose9 wrote:
st.detail.Name = TEXT("Williams"); st.detail.Address = TEXT("B-33 Lane 5");
You probably meant to use
_tcscpy()
here instead."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
#include <windows.h>
#include <conio.h>
#include <iostream>using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};int main()
{DWORD BytesWritten=0, BytesRead=0; Student st,read; st.code =1; st.detail.Name = TEXT("Williams"); st.detail.Age = 25; st.detail.Address = TEXT("B-33 Lane 5"); HANDLE hFile = CreateFile(TEXT("C:\\\\demo.txt"),GENERIC\_READ | GENERIC\_WRITE,0,0,OPEN\_ALWAYS,FILE\_ATTRIBUTE\_NORMAL,0); if (hFile != INVALID\_HANDLE\_VALUE) { WriteFile(hFile,&st,sizeof(Student),&BytesWritten,0); ReadFile(hFile,&read,sizeof(Student),&BytesRead,0); CloseHandle(hFile); cout<<read.code<<endl; cout<<read.detail.Name; } else { cout<<"Error! Cannot Open File"; exit(1); } \_getche(); return (0);
}
when executing this code following error occurs.
Error 1 error C2440: '=' : cannot convert from 'const wchar_t [9]' to 'wchar_t [25]'
Error 2 error C2440: '=' : cannot convert from 'const wchar_t [12]' to 'wchar_t [100]'Some Day I Will Prove MySelf :: GOLD
Hi,
goldenrose9 wrote:
when executing this code following error occurs.
When compiling is not when executing. 1 You do not correctly initialize your
st
structure, change to:Student st = {{TEXT("Williams"), 25, TEXT("B-33 Lane 5")}, 1 }, read = {0};
2 Your code will then compile but not to your expected result, you must set the file pointer to
FILE_BEGIN
before reading:WriteFile(hFile,&st,sizeof(Student),&BytesWritten,0); SetFilePointer(hFile, 0, 0, FILE\_BEGIN); ReadFile(hFile,&read,sizeof(Student),&BytesRead,0);
cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
goldenrose9 wrote:
st.detail.Name = TEXT("Williams"); st.detail.Address = TEXT("B-33 Lane 5");
You probably meant to use
_tcscpy()
here instead."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
DavidCrow wrote:
You probably meant to use _tcscpy() here instead.
This works like a charm
_tcscpy(st.detail.Name,TEXT("Williams"));
_tcscpy(st.detail.Address,TEXT("B-33 Lane 5"));But another problem arises cannot set values in long and DWORD variables
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};Student st;
st.code = 1;but st.code stores a default value of -16843010 and same problem with long data type st.Details.Age = 25 stores 65278, instead of 25.
:confused: :wtf: MODIFIED: _tcscpy_s() now works. i was making a small mistake. Now it works.._tcscpy_s(st.detail.Name,_tcslen(st.detail.Name),TEXT("WILLIAMS"));
Some Day I Will Prove MySelf :: GOLD
modified on Wednesday, February 16, 2011 10:43 PM
-
DavidCrow wrote:
You probably meant to use _tcscpy() here instead.
This works like a charm
_tcscpy(st.detail.Name,TEXT("Williams"));
_tcscpy(st.detail.Address,TEXT("B-33 Lane 5"));But another problem arises cannot set values in long and DWORD variables
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};Student st;
st.code = 1;but st.code stores a default value of -16843010 and same problem with long data type st.Details.Age = 25 stores 65278, instead of 25.
:confused: :wtf: MODIFIED: _tcscpy_s() now works. i was making a small mistake. Now it works.._tcscpy_s(st.detail.Name,_tcslen(st.detail.Name),TEXT("WILLIAMS"));
Some Day I Will Prove MySelf :: GOLD
modified on Wednesday, February 16, 2011 10:43 PM
goldenrose9 wrote:
why error is occurring in _tcscpy_s()
Hard to say since you did not indicate what the error was.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
DavidCrow wrote:
You probably meant to use _tcscpy() here instead.
This works like a charm
_tcscpy(st.detail.Name,TEXT("Williams"));
_tcscpy(st.detail.Address,TEXT("B-33 Lane 5"));But another problem arises cannot set values in long and DWORD variables
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};Student st;
st.code = 1;but st.code stores a default value of -16843010 and same problem with long data type st.Details.Age = 25 stores 65278, instead of 25.
:confused: :wtf: MODIFIED: _tcscpy_s() now works. i was making a small mistake. Now it works.._tcscpy_s(st.detail.Name,_tcslen(st.detail.Name),TEXT("WILLIAMS"));
Some Day I Will Prove MySelf :: GOLD
modified on Wednesday, February 16, 2011 10:43 PM
-
#include <windows.h>
#include <conio.h>
#include <iostream>using namespace std;
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};int main()
{DWORD BytesWritten=0, BytesRead=0; Student st,read; st.code =1; st.detail.Name = TEXT("Williams"); st.detail.Age = 25; st.detail.Address = TEXT("B-33 Lane 5"); HANDLE hFile = CreateFile(TEXT("C:\\\\demo.txt"),GENERIC\_READ | GENERIC\_WRITE,0,0,OPEN\_ALWAYS,FILE\_ATTRIBUTE\_NORMAL,0); if (hFile != INVALID\_HANDLE\_VALUE) { WriteFile(hFile,&st,sizeof(Student),&BytesWritten,0); ReadFile(hFile,&read,sizeof(Student),&BytesRead,0); CloseHandle(hFile); cout<<read.code<<endl; cout<<read.detail.Name; } else { cout<<"Error! Cannot Open File"; exit(1); } \_getche(); return (0);
}
when executing this code following error occurs.
Error 1 error C2440: '=' : cannot convert from 'const wchar_t [9]' to 'wchar_t [25]'
Error 2 error C2440: '=' : cannot convert from 'const wchar_t [12]' to 'wchar_t [100]'Some Day I Will Prove MySelf :: GOLD
st.detail.Name = TEXT("Williams");
You cannot copy characters in this way, you must use one of the copy methods.
I must get a clever new signature for 2011.
-
Try ZeroMemory
Student st,read;
ZeroMemory(&st,sizeof(st));I believe in LOVE AT FIRST SIGHT... Bcoz I have loved my Mother... even since I opened my eyes...(ICAN)
-
DavidCrow wrote:
You probably meant to use _tcscpy() here instead.
This works like a charm
_tcscpy(st.detail.Name,TEXT("Williams"));
_tcscpy(st.detail.Address,TEXT("B-33 Lane 5"));But another problem arises cannot set values in long and DWORD variables
struct Detail{
wchar_t Name[25];
long Age;
wchar_t Address[100];
};struct Student{
Detail detail;
DWORD code;
};Student st;
st.code = 1;but st.code stores a default value of -16843010 and same problem with long data type st.Details.Age = 25 stores 65278, instead of 25.
:confused: :wtf: MODIFIED: _tcscpy_s() now works. i was making a small mistake. Now it works.._tcscpy_s(st.detail.Name,_tcslen(st.detail.Name),TEXT("WILLIAMS"));
Some Day I Will Prove MySelf :: GOLD
modified on Wednesday, February 16, 2011 10:43 PM
goldenrose9 wrote:
i was making a small mistake.
And you still are.
_tcslen()
does not tell you the size/capacity ofName
, but rather how many characters it is currently holding."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
goldenrose9 wrote:
i was making a small mistake.
And you still are.
_tcslen()
does not tell you the size/capacity ofName
, but rather how many characters it is currently holding."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
yes you are right. but when i use
_tcscpy_s(st.detail.Name,sizeof(st.detail.Name),TEXT("WILLIAMS"));
stack around
st
get corrupted. moreover when i initialize st withst.code =1;
st.detail.Age = 25;then the value is changed to
st.code = 4278124286
st.detail.Age = -16843010i had intialized the st as given
st.code =1;
st.detail.Age = 25;
_tcscpy_s(st.detail.Name,sizeof(st.detail.Name),TEXT("WILLIAMS"));
_tcscpy_s(st.detail.Address,sizeof(st.detail.Address),TEXT("BB-33 LANE 5"));Some Day I Will Prove MySelf :: GOLD
-
yes you are right. but when i use
_tcscpy_s(st.detail.Name,sizeof(st.detail.Name),TEXT("WILLIAMS"));
stack around
st
get corrupted. moreover when i initialize st withst.code =1;
st.detail.Age = 25;then the value is changed to
st.code = 4278124286
st.detail.Age = -16843010i had intialized the st as given
st.code =1;
st.detail.Age = 25;
_tcscpy_s(st.detail.Name,sizeof(st.detail.Name),TEXT("WILLIAMS"));
_tcscpy_s(st.detail.Address,sizeof(st.detail.Address),TEXT("BB-33 LANE 5"));Some Day I Will Prove MySelf :: GOLD
Have you considered:
st.code = 1;
st.st.Age = 25;
_tcscpy_s(st.st.Name, sizeof(st.st.Name), TEXT("WILLIAMS"));
_tcscpy_s(st.st.Address, sizeof(st.st.Address), TEXT("BB-33 LANE 5"));For clarities sake, you might consider renaming the
st
member ofStudent
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Have you considered:
st.code = 1;
st.st.Age = 25;
_tcscpy_s(st.st.Name, sizeof(st.st.Name), TEXT("WILLIAMS"));
_tcscpy_s(st.st.Address, sizeof(st.st.Address), TEXT("BB-33 LANE 5"));For clarities sake, you might consider renaming the
st
member ofStudent
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
whenever
st.code
andst.st.Age
is initialized with any value the result is,st.code = 4278124286
st.detail.Age = -16843010Some Day I Will Prove MySelf :: GOLD
-
whenever
st.code
andst.st.Age
is initialized with any value the result is,st.code = 4278124286
st.detail.Age = -16843010Some Day I Will Prove MySelf :: GOLD
You need to look more closely at the code snippet I provided. You should not be assigning or referencing
st.Detail
. In yourStudent
structure,Detail
is a type of structure, not an instance of one."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
You need to look more closely at the code snippet I provided. You should not be assigning or referencing
st.Detail
. In yourStudent
structure,Detail
is a type of structure, not an instance of one."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
please give a small example. :confused:
Some Day I Will Prove MySelf :: GOLD
-
please give a small example. :confused:
Some Day I Will Prove MySelf :: GOLD
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
on using
st.code = 1;
st.st.Age = 25;
_tcscpy_s(st.st.Name, sizeof(st.st.Name), TEXT("WILLIAMS"));
_tcscpy_s(st.st.Address, sizeof(st.st.Address), TEXT("BB-33 LANE 5"));following error occurs.
Error 1 error C2039: 'st' : is not a member of 'Student'
Error 2 error C2228: left of '.Age' must have class/struct/unionSome Day I Will Prove MySelf :: GOLD