a simple question about files

This is a discussion on a simple question about files within the C++ Programming forums, part of the General Programming Boards category; I am learning files in c++. I wrote this program . Code: #include <fstream.h> #include <iostream.h> struct st{ char name[21]; ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    177

    a simple question about files

    I am learning files in c++.
    I wrote this program .
    Code:
    #include <fstream.h>
    #include <iostream.h>
    struct st{
    char name[21];
    int age;}test;
    int main(){
    int i;
    ofstream fp("myfile.txt");
    if(!fp){
      cout << "error";
      return 0;}
    for(i=0;i<3;++i){
      cin>>test.name;
      cin>>test.age;
      if(test.age>30)
        fp.write((char*)&test,sizeof(struct st));}
    fp.close();
    return (0);}
    my problem is when I check the file (myfile) after running this program there is no age in it .
    for example if I enter 40 the program get it as aski code and write '(' instead of 40 in the file .
    what can I do?

    my another question is : if I have c++ compiler in "c:\bin" is it possible to make the file in another place for exampl in D drive ?

    tnx

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    i can only answer your second question. of course you can unless you have a really crappy compiler which i doubt. by the way, which compiler are you running?

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    it is an old one .
    dont laugh at me .
    when I started learning c++ some one told me borland 3.1 is the best for getting start and therefore I installed it .

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    get dev-c++, a links on this site but i'll give it to you anyways www.bloodshed.net download it for free, really good

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    thanks

  6. #6
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Code:
    fstream openf;
    string filename;
    ......... .........
    filename="c:\ooo\111\222\defa.ooo";
    ..........
    openf.open(filename.c_str(),fstream::in|fstream::out);
    Last edited by Hermitsky; 09-24-2004 at 08:46 AM.

    blow me ... ...

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    thanks alot , but what about my first question ?

  8. #8
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    you can put yout input directly into file,do not use struct

    blow me ... ...

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    I know that way but but if I have to use struct ?

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    change this:

    sizeof(struct st)

    to this:

    sizeof(test)

    and see if that clears up your problem. The write method will write to the file in binary, which will probably show up as hexadecimal symbols when you try to view the file. If so you will need to read the file back to your program to see if it has the correct information in it.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > for example if I enter 40 the program get it as aski code and write '(' instead of 40 in the file .
    Well that's what you get for writing binary data rather than text

    If you want to read it properly, do
    fp << test.name << " " << test.age << endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Nov 2003
    Posts
    177
    thank you all .

  13. #13
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by Hermitsky
    Code:
    fstream openf;
    string filename;
    ......... .........
    filename="c:\ooo\111\222\defa.ooo";
    ..........
    openf.open(filename.c_str(),fstream::in|fstream::out);
    I think you meant

    Code:
    filename="c:\\ooo\\111\\222\\defa.ooo";
    
    or
    
    filename="c:/ooo/111/222/defa.ooo";
    A single backspace in a C\C++ string represents an escape sequence, there fore will not get the expected result even if your compiler lets it compile.

    You could also do this

    Code:
    bool WriteToFile ( const char *file_name )
    {
    
        ostream the_file ( file_name );
    
        // Check if it opened/existed
        if ( !the_file.is_open () ) {
    
            // if it didnt
            return false;
    
        }
    
        // write to it
    
        the_file.close ();
        return true;
    
    }
    
    // to use the function
    
    int main ()
    {
    
        if ( !WriteToFile ( "C:/MyFile.txt" ) )
            exit ( 1 );
    
        return 0;
    
    }
    That should give you a rough idea of things you can do.
    Last edited by Vicious; 09-25-2004 at 12:00 AM.
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 05:29 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. files question
    By Micko in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2004, 03:12 PM
  5. Question about ".o" files
    By Jez_Master in forum C++ Programming
    Replies: 1
    Last Post: 04-11-2002, 02:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21