Reading & Writing files (Error)

This is a discussion on Reading & Writing files (Error) within the C++ Programming forums, part of the General Programming Boards category; I have some code to save a map: Code: void Save() { COORD Pos; HANDLE hOt = GetStdHandle(STD_OUTPUT_HANDLE); DWORD reader; ...

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Reading & Writing files (Error)

    I have some code to save a map:

    Code:
    void Save() { 
     COORD Pos;
     HANDLE hOt = GetStdHandle(STD_OUTPUT_HANDLE);
     DWORD reader;
     char buffer[2028];
     char DELO[1];
     int x_pos, y_pos;
     SHORT bc;
     
     for(bc=0; bc < 2028; bc++)
      DELO[0] = ' ';
    
     bc=0;
     ofstream file_out(MAINPATHNAME);
     file_out << "";
     if(!file_out) {
      clrscr();
      cout << "Unable to open file!";
      }
     for(y_pos=0; y_pos <= 24; y_pos++)
      for(x_pos=0; x_pos <= 80; x_pos++){
       bc++;
       Pos.X = x_pos;
       Pos.Y = y_pos;
       ReadConsoleOutputCharacter(hOt, &DELO[0], 1, Pos, &reader);
       if(x_pos == 80) {
        buffer[bc] = 'W';
      } else 
       buffer[bc] = DELO[0];
     }
     for(bc=0; bc < 2028; bc++) {
      if(buffer[bc] == 'W')
       file_out << endl;
      else
       file_out << buffer[bc];
     }
     file_out.close();
     clrscr();
     exit(0);
    }
    And some code to load it:

    Code:
    void load() {
     char DELO[1];
     SHORT x;
    
     ifstream file_in(MAINPATHNAME);
     
     if(!file_in)
      cout << "cannot open file.";
    
     for(x=0; x < 2050; x++) {
      if(!file_in.eof())
       break;
      file_in.getline(map, 1);
     }
    
     cout << map;
    }
    Now, when I save the map, it saves, but it saves some weird characters that shouldent be there also...

    And when I try to load, it doesnt work at all? I'm a noob with files :/. I'm sure I messed something simple up, but I cant find any help on the matter. :/.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    The N00b That Owns You!
    Join Date
    Jul 2005
    Location
    Canada!
    Posts
    178
    i havent done File I/O for a long LONG time

    but instead of ofstream file_out(MAINPATHNAME)

    try:

    Code:
     
    ofstream file_out;
    file_out.open(MAINPATHNAME)
    
    if (file_out.fail())
    {
    error
    }
    
    output data
    
    file_out.close(); .
    
    also
    
    ifstream file_in;
    
    file_in.open(MAINPATHNAME, ios::nocreate)
    
    if(!file_in.is_open())
    {
    error
    }
    
    Read Data
    
    file_in.close();
    i hoped that helped a bit iom on a tight sched so i dont have time to examine all of your source code i forgot how to read and output data sorry :P
    New Function!!!!

    glAddIdol(C+noob);

    The feeling of rusty spoons against my salad fingers is almost ORGASMIC

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Tried editing my code to add that, didint help :/.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Code:
    if(!file_in.eof())
       break;
    Is that really what you want to do? Break if it is not the end of file?

    You should use the return value of getline (or get() or operator >> if you end up using those) instead of checking for eof() anyway.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Thanks for the code idea but, I updated my code. Doesnt work correctly though, doesnt seem to want to load up ENTERS and SPACES (Though they are there in the saved file).

    It loads up fine otherwise, but the spacing is wrong? Is this an inbuilt thing with file loading? (Should be an ENTER every 80 characters.)

    Code:
    #include <fstream.h>
    #include <windows.h>
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    using namespace std;
    
    #define MAINPATHNAME "C:\\Mazer\\Mazer1.txt"
    #define Peice1 '='
    #define Peice2 '['
    #define Peice3 ']'
    #define Peice4 '@' //exit
    #define Peice5 ' '
    #define Peices 4
    
    char map[2043];
    
    void LOADMAP();
    
    int main() {
     clrscr();
     cout << "";
     LOADMAP();
    }
    
    void LOADMAP() { //26
     char DELO[1];
     SHORT x, x2;
    
     ifstream file_in(MAINPATHNAME, ios::nocreate);
     
     if(!file_in)
      cout << "cannot open file.";
    
     for(x=0; x < 2043; x++)
      file_in >> map[x];
    
     for(x=0; x < 2043; x++)
      cout << map[x];
    }
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It doesn't load up whitespace because >> skips whitespace. Use get instead to read one character at a time.

    BTW, <fstream.h> and <iostream.h> are non-standard headers that don't work on modern compilers. You already have the using namespace std part, so change those to <fstream> and <iostream>. While you are at it, consider switching to C++ strings instead of C style character arrays.

  7. #7
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I use strings if I dont explicitly know the size of my object. In my mind, strings would decrease speed because they keep needing to expand. Though if its a random size, strings for the win!

    And, how does get() work? It doesnt seem to do much when I use it. It doesnt like when I use get(char array) either :/.

    My updated code:
    Code:
    #include <fstream>
    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    #include <stdlib.h>
    using namespace std;
    
    #define MAINPATHNAME "C:\\Mazer\\Mazer1.txt"
    #define Peice1 '='
    #define Peice2 '['
    #define Peice3 ']'
    #define Peice4 '@' //exit
    #define Peice5 ' '
    #define Peices 4
    
    char map[2043];
    
    void LOADMAP();
    
    int main() {
     clrscr();
     cout << "";
     LOADMAP();
    }
    
    void LOADMAP() { //26
     char DELO[1];
     SHORT x, x2;
    
     ifstream file_in(MAINPATHNAME, ios::nocreate);
     
     if(!file_in)
      cout << "cannot open file.";
    
     for(x=0; x < 2043; x++)
      file_in.get() >> map[x]; //Cant find any other way to use get() :/
    
     for(x=0; x < 2043; x++)
      cout << map[x];
    }
    Doesnt do anything? -,-. I'm very confused.
    Last edited by Blackroot; 01-09-2006 at 07:40 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    You can call get by passing the variable to the function. For example:
    Code:
    char c;
    cin.get(c);
    Since map[x] is a single char, you would use that.

    You can still use strings even if you know the size of your data, just use reserve, resize or the constructor to set the initial size and it won't have to resize.

  9. #9
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    !! It worked!! But for some reason it's overspacing now >.>. Every line has one extra space for some weird reason...? Well, it works now! (Scratch that, when I saved the map, it somehow added an extra character, fixed it though!)

    Just one question left, how does get() work? It doesnt need an integer, so should I assume it supplies a counter for you?

    Well, thank you very much for putting up with all my noobieness .
    Last edited by Blackroot; 01-09-2006 at 11:12 PM.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Get just gets the next character in the stream, so it is just getting one character every time no matter what. It is more basic than getline or operator>> since it doesn't care what the character is, it just reads it into the variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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