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. Problem in struct

Problem in struct

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 3 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.
  • N Offline
    N Offline
    NewVC
    wrote on last edited by
    #1

    Hi All I got example of get file size from MSDN.But i have a problem to write out put in file.How to convert struct values to CString or any other solution. Example code is here

    #include <time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <errno.h>

    int main( void )
    {
    struct _stat buf;
    int result;
    char timebuf[26];
    char* filename = "crt_stat.c";
    errno_t err;

    // Get data associated with "crt_stat.c":
    result = _stat( filename, &buf );

    // Check if statistics are valid:
    if( result != 0 )
    {
    perror( "Problem getting information" );
    switch (errno)
    {
    case ENOENT:
    printf("File %s not found.\n", filename);
    break;
    case EINVAL:
    printf("Invalid parameter to _stat.\n");
    break;
    default:
    /* Should never be reached. */
    printf("Unexpected error in _stat.\n");
    }
    }
    else
    {
    // Output some of the statistics:
    printf( "File size : %ld\n", buf.st_size );
    printf( "Drive : %c:\n", buf.st_dev + 'A' );
    err = ctime_s(timebuf, 26, &buf.st_mtime);
    if (err)
    {
    printf("Invalid arguments to ctime_s.");
    exit(1);
    }
    printf( "Time modified : %s", timebuf );
    }
    }

    How can i write it's values buf.st_size in file. Plz help me

    I M 2 Replies Last reply
    0
    • N NewVC

      Hi All I got example of get file size from MSDN.But i have a problem to write out put in file.How to convert struct values to CString or any other solution. Example code is here

      #include <time.h>
      #include <sys/types.h>
      #include <sys/stat.h>
      #include <stdio.h>
      #include <errno.h>

      int main( void )
      {
      struct _stat buf;
      int result;
      char timebuf[26];
      char* filename = "crt_stat.c";
      errno_t err;

      // Get data associated with "crt_stat.c":
      result = _stat( filename, &buf );

      // Check if statistics are valid:
      if( result != 0 )
      {
      perror( "Problem getting information" );
      switch (errno)
      {
      case ENOENT:
      printf("File %s not found.\n", filename);
      break;
      case EINVAL:
      printf("Invalid parameter to _stat.\n");
      break;
      default:
      /* Should never be reached. */
      printf("Unexpected error in _stat.\n");
      }
      }
      else
      {
      // Output some of the statistics:
      printf( "File size : %ld\n", buf.st_size );
      printf( "Drive : %c:\n", buf.st_dev + 'A' );
      err = ctime_s(timebuf, 26, &buf.st_mtime);
      if (err)
      {
      printf("Invalid arguments to ctime_s.");
      exit(1);
      }
      printf( "Time modified : %s", timebuf );
      }
      }

      How can i write it's values buf.st_size in file. Plz help me

      I Offline
      I Offline
      Iain Clarke Warrior Programmer
      wrote on last edited by
      #2

      I'm not quite sure what you're after, but in pure C stuff, you can write values to a file like so:

      FILE *f;
      ...
      f = fopen ("c:\\myfile.abc", "w"); // makes a new empty file.
      ...
      fprintf (f, "Some Integer: %i\n", nMyInt); // could be buf.st_size if you like
      ...
      fclose (f);

      I hope that puts you on the right path, Iain.

      In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!

      1 Reply Last reply
      0
      • N NewVC

        Hi All I got example of get file size from MSDN.But i have a problem to write out put in file.How to convert struct values to CString or any other solution. Example code is here

        #include <time.h>
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <stdio.h>
        #include <errno.h>

        int main( void )
        {
        struct _stat buf;
        int result;
        char timebuf[26];
        char* filename = "crt_stat.c";
        errno_t err;

        // Get data associated with "crt_stat.c":
        result = _stat( filename, &buf );

        // Check if statistics are valid:
        if( result != 0 )
        {
        perror( "Problem getting information" );
        switch (errno)
        {
        case ENOENT:
        printf("File %s not found.\n", filename);
        break;
        case EINVAL:
        printf("Invalid parameter to _stat.\n");
        break;
        default:
        /* Should never be reached. */
        printf("Unexpected error in _stat.\n");
        }
        }
        else
        {
        // Output some of the statistics:
        printf( "File size : %ld\n", buf.st_size );
        printf( "Drive : %c:\n", buf.st_dev + 'A' );
        err = ctime_s(timebuf, 26, &buf.st_mtime);
        if (err)
        {
        printf("Invalid arguments to ctime_s.");
        exit(1);
        }
        printf( "Time modified : %s", timebuf );
        }
        }

        How can i write it's values buf.st_size in file. Plz help me

        M Offline
        M Offline
        Michael Schubert
        wrote on last edited by
        #3

        Something like this:

        CString csOut;
        csOut.Format("File size : %ld\n", buf.st_size);

        Then use CStdioFile::WriteString() to write to a file.

        N 1 Reply Last reply
        0
        • M Michael Schubert

          Something like this:

          CString csOut;
          csOut.Format("File size : %ld\n", buf.st_size);

          Then use CStdioFile::WriteString() to write to a file.

          N Offline
          N Offline
          NewVC
          wrote on last edited by
          #4

          Thanks Michael

          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