Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. structure to file [REMAIN UNSOLVED] [CLOSED]

structure to file [REMAIN UNSOLVED] [CLOSED]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
22 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G goldenrose9

    #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

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #11

    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.

    1 Reply Last reply
    0
    • S ShilpiP

      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)

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #12

      To OP: This is only meaningful if you don't intend to initialize every member separately.

      home

      1 Reply Last reply
      0
      • G goldenrose9

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #13

        goldenrose9 wrote:

        i was making a small mistake.

        And you still are. _tcslen() does not tell you the size/capacity of Name, 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

        G 1 Reply Last reply
        0
        • D David Crow

          goldenrose9 wrote:

          i was making a small mistake.

          And you still are. _tcslen() does not tell you the size/capacity of Name, 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

          G Offline
          G Offline
          goldenrose9
          wrote on last edited by
          #14

          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 with

          st.code =1;
          st.detail.Age = 25;

          then the value is changed to

          st.code = 4278124286
          st.detail.Age = -16843010

          i 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

          D 1 Reply Last reply
          0
          • G goldenrose9

            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 with

            st.code =1;
            st.detail.Age = 25;

            then the value is changed to

            st.code = 4278124286
            st.detail.Age = -16843010

            i 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

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #15

            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 of Student.

            "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

            G 1 Reply Last reply
            0
            • D David Crow

              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 of Student.

              "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

              G Offline
              G Offline
              goldenrose9
              wrote on last edited by
              #16

              whenever st.code and st.st.Age is initialized with any value the result is,

              st.code = 4278124286
              st.detail.Age = -16843010

              Some Day I Will Prove MySelf :: GOLD

              D 1 Reply Last reply
              0
              • G goldenrose9

                whenever st.code and st.st.Age is initialized with any value the result is,

                st.code = 4278124286
                st.detail.Age = -16843010

                Some Day I Will Prove MySelf :: GOLD

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #17

                You need to look more closely at the code snippet I provided. You should not be assigning or referencing st.Detail. In your Student 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

                G 1 Reply Last reply
                0
                • D David Crow

                  You need to look more closely at the code snippet I provided. You should not be assigning or referencing st.Detail. In your Student 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

                  G Offline
                  G Offline
                  goldenrose9
                  wrote on last edited by
                  #18

                  please give a small example. :confused:

                  Some Day I Will Prove MySelf :: GOLD

                  D 1 Reply Last reply
                  0
                  • G goldenrose9

                    please give a small example. :confused:

                    Some Day I Will Prove MySelf :: GOLD

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #19

                    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

                    G 1 Reply Last reply
                    0
                    • D David Crow

                      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

                      G Offline
                      G Offline
                      goldenrose9
                      wrote on last edited by
                      #20

                      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/union

                      Some Day I Will Prove MySelf :: GOLD

                      D 1 Reply Last reply
                      0
                      • G goldenrose9

                        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/union

                        Some Day I Will Prove MySelf :: GOLD

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #21

                        I just compiled your code with my changes. It compiled and ran fine.

                        "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

                        G 1 Reply Last reply
                        0
                        • D David Crow

                          I just compiled your code with my changes. It compiled and ran fine.

                          "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

                          G Offline
                          G Offline
                          goldenrose9
                          wrote on last edited by
                          #22

                          please give a sample so that i can understand my mistake.

                          Some Day I Will Prove MySelf :: GOLD

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups