Thread: int data from file

  1. #1
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63

    int data from file

    How could I get int data from a file? Im trying to use files better, as I said on my "checking existance" post, but I ran into another problem completely aside from existance. Im trying to load int data from a txt, but because it is in a char variable I cant use any operations on them. Once again, Im asking if theres a simple way to do this, or if Im gonna have to make another custom function for this, too.

    Note: Ive tried the (int) before it, but instead of the same thing, the number is something in the millions

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Code:
    ifstream in("file");
    int foo;
    in >> foo;
    cout << foo << endl;

  3. #3
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    That didnt work when I tried it, but I found it in the faq anyways:

    Code:
    char* charnum="376"[4];
    int intnum;
    intnum=atoi(charnum);

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Depends on how you wrote the int to the file:

    If written to a file as text:
    Code:
    //Write int to file
    ofstream output("data.txt");
    int val = 10;
    output << val;
    output.close();
    
    //Read int from file
    ifstream input("data.txt");
    int val;
    input >> val;
    input.close();
    If written as binary data:
    Code:
    //Write int to file
    ofstream output("data.txt",ios::binary|ios::out);
    int val = 10;
    output.write((char*)&val,sizeof(val));
    output.close();
    
    //Read int from file
    ifstream input("data.txt",ios::binary|ios::in);
    int val;
    input.read((char*)&val,sizeof(val));
    input.close();
    Quote Originally Posted by LloydUzari
    That didnt work when I tried it, but I found it in the faq anyways:

    Code:
    char* charnum="376"[4];
    int intnum;
    intnum=atoi(charnum);
    What does that have to do with file i/o???? Sounds like you were really asking how to convert a character array (string) or string container storing numerical values into an int data type.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    That was just an example, I took the char variable I got from reading the file, and changed it using that.

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    How are you inputing it if it is a number? You should have no trouble doing the methods shown above. Could you show what you are doing it sounds like your making it harder then it really is?
    Woop?

  7. #7
    C++ n00bie :D
    Join Date
    Jul 2004
    Posts
    63
    Its most likely because of the way Im using. Im using the get subfunc so it will check the whole line instead of just one word, but it doesnt allow using an int variable. I just needed a way to get an int from it, so I think I found it, unless theres a better way :/
    Code:
    void loadset(const char *filename) {
      char* num1=new char[100];
      char* num2=new char[100];
      char* num3=new char[100];
      int numONE, numTWO, numTHREE;
      int location[5];
      std::ifstream loadline(filename);
      loadline>>location[1];
      loadline>>location[2];
      loadline.seekg(10);
      loadline.get(num1, 100, '\n');
      loadline.seekg(17 + location[1]);
      loadline.get(num2, 100, '\n');
      loadline.seekg(24 + location[2]);
      loadline.get(num3, 100, '\n');
      numONE=atoi(num1);
      numTWO=atoi(num2);
      numTHREE=atoi(num3);
      std::cout<<"Num1: "<<numONE<<std::endl;
      std::cout<<"Num2: "<<numTWO<<std::endl;
      std::cout<<"Num3: "<<numTHREE<<std::endl;
      loadline.close();
    }//loadset()
    Thats whats there right now. It seems to work well, but it does seem to slow the cout down a bit for some reason. So you know what the file looks like:
    Code:
    3 5
    Num1=376
    Num2=29
    Num3=2
    The numbers at the top tell the program how many extra characters to skip.(Note I did use the >> for the numbers up top cause it seemed easier, but the others Im using like that cause in the accual version I am going to use numbers in some slots, and char in others.)

    Note: Thats just to test if its working, currently Im working on an address book like program, and am trying that setup.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM