Thread: Put a Number from a .txt file to a int variable ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Put a Number from a .txt file to a int variable ?

    I have a .txt file(file1) that contains one number: 1

    What I want to do now is in my code below put this Number that in this case is: 1 into the variable: "int tal = 0"
    How is it possible to do this ? I suppose the number 1 is considered to be a string in the textfile ?
    Thanks.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    
    using namespace std;
    
    int main () 
    
    { char Number[20];
      int tal = 0;
      ofstream Test;
      Test.open ("file2.txt");
      ifstream myfile ("file1.txt");
      myfile >> Number;
    
      Test << tal <<"\n";
      return 0;
    }
    Last edited by Coding; 12-16-2007 at 06:27 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In this case, myfile >> tal would do fine, I believe.
    And post in the appropriate forum next time, if you did it on purpose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Moved to the C++ board.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Elysia

    Thank You it did work...
    But haven&#180;t I posted in the right forum for this question ?
    My question is under "Forum: C++ Programming"

    And if not what is the link to the right place to post C++ questions ?
    Thanks

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You originally posted this in "Projects and Job Recruitment".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Coding View Post
    Elysia

    Thank You it did work...
    But havenīt I posted in the right forum for this question ?
    My question is under "Forum: C++ Programming"

    And if not what is the link to the right place to post C++ questions ?
    Thanks
    It is in that place right now - but I presume based on laserlight's comment that it wasn't previously and that it's now been moved.

    By the way, the contents of a file isn't "considered a string" - it's just a stream of bytes to the C++ runtime library. You can then ask the C++ runtime to read it as an integer, string, float, bool. If you implement your own class, you can (if you like) implement your own function to read the entire class content in from a stream. For example, if you make your own complex class, the input of that could be two reads of a double.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes now I understand. I will put questions under "C++ Programming" I thought I did that but now I know.
    Ok, I think I follow your description.. I will experiment with this...

    Coding

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Be careful when using operators << and >> with streams. They're interpreted as text and should probably be read back as text too, otherwise the offset might not match when you read data back.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Be careful when using operators << and >> with streams. They're interpreted as text and should probably be read back as text too, otherwise the offset might not match when you read data back.
    You mean "you shouldn't store binary data when using >>" - sure, that's not what I meant either.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but reading back into a non-string buffer may also cause problems.
    Code:
    // Assume ofile is ostream.
    file << "120" << "120";
    // Assume ifile is an istream
    UINT64 num, num2;
    file >> num;
    file >> num2;
    num will probably be 120120 (or something like that) and num2 will fail.
    Last edited by Elysia; 12-17-2007 at 10:35 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM