Assigning values from a file

This is a discussion on Assigning values from a file within the C++ Programming forums, part of the General Programming Boards category; I need a part of my program to read an integer value from a text file (there's nothing else in ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    37

    Assigning values from a file

    I need a part of my program to read an integer value from a text file (there's nothing else in that file) and assign the found value to a number. That number (bookspan) is not a member of a class...is this operation possible? I've written part of the code, but cant understand what to put in the critical part.
    Code:
    int bookspan;
    int main()
    {
         Entry e[bookspan];
         ifstream readspan;
         readspan.open("span.txt", ios::in);
         while(!readspan.eof())
         {
                        readspan.seekg(0);
                        bookspan=bookspan; //Oh wait, that doesn't make any sense...
                        readspan.close();
         }
         cout<<"\nBookspan is "<<bookspan;
         cin>>bookspan;  //Makeshift 'pause my program' line
         return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What would you do if you wanted to read it from the console? If we consider that the console is an input stream, how would you change that to use your ifstream?

    --
    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.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by matsp View Post
    What would you do if you wanted to read it from the console? If we consider that the console is an input stream, how would you change that to use your ifstream?

    --
    Mats
    That points to 'readspan' here.
    But all the same...
    Code:
    int main()
    {
         int in;
         Entry e[bookspan];
         ifstream readspan;
         readspan.open("span.txt", ios::in);
         while(!readspan.eof())
         {
                        readspan.seekg(0);
                        readspan>>bookspan;
         }
         readspan.close();
         cout<<"\nBookspan is "<<bookspan;
         cin>>bookspan;
         return 0;
    }
    ...It worked! O_O Whoopee, thanks for the nudge in the right direction.
    -regards,
    ultrabot90

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Glad to be of help.

    Code:
         Entry e[bookspan];
    This is probably not what you want - bookspan is zero, which creates a "no entry array" - not very useful.

    --
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    37
    Quote Originally Posted by matsp View Post
    Glad to be of help.

    Code:
         Entry e[bookspan];
    This is probably not what you want - bookspan is zero, which creates a "no entry array" - not very useful.

    --
    Mats
    I saw that bit, thanks for pointing it out...But this is just a part of the whole program.
    1. This is function will be executed every time the program starts. (And the default value in span.txt is 50 - after bookspan gets the value, the array changes automatically)
    2. Anytime bookspan is increased, it'll be written to span.txt
    3. So when the program starts again, it'll remember the last value.
    -regards,
    ultrabot90

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ultrabot90 View Post
    I saw that bit, thanks for pointing it out...But this is just a part of the whole program.
    1. This is function will be executed every time the program starts. (And the default value in span.txt is 50 - after bookspan gets the value, the array changes automatically)
    2. Anytime bookspan is increased, it'll be written to span.txt
    3. So when the program starts again, it'll remember the last value.
    -regards,
    ultrabot90
    Sure, but the posted code has the creation of the array before you've read the file. So the bookspan variable would be zero. At the very least you need to move it. I'm not entirely sure if you are actually allowed to have a variable to size an array, I think that's an extension that only exists in some compilers. You should use "new" to allocate variable amount of memory.

    --
    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
    Sep 2007
    Posts
    37
    Quote Originally Posted by matsp View Post
    At the very least you need to move it.
    Yep I did that in the main code.
    Quote Originally Posted by matsp View Post
    I'm not entirely sure if you are actually allowed to have a variable to size an array, I think that's an extension that only exists in some compilers.
    Yeah, I noticed how I couldnt do it in Borland C++ v2 (MS DOS sorta thing)
    Quote Originally Posted by matsp View Post
    You should use "new" to allocate variable amount of memory.
    I'm afraid I dunno how to do that for arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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