But for some reason I have programmer's block and am not quite sure of how to do it.  I guess that's what happens when you've been looking at code for three days straight, all day long.  I have attempted to do so by trying to force myself to think creatively, but my attempt is quite obviously flawed.
For instance, I have a variable called "fullscreen_x".  I want this variable to be assigned information from a specific point on a specific line of a text file (like a user configuration file).
Basically, I just want to know how to read information from a text file.  Thanks
In Visual C++, I want to read a string from a text file, and assign it to a variable?
Fred: A disgrace how you answer straight away with full code; you are encouraging laziness. Give hints and algorithms and let the asker implement them rather than showing up.
For the asker:
one of many variation to solve your problem would be:
1.) Read the file
2.) store the content into a data structure (vector, list ...etc.)
3.) Implement a search module which will iterate through that data structure
4.) Tokenize the search result and format it
5.) Assign that result to the variable
Reply:I do it somewhere in there.....I'm to lasy to pick it out....its part of fstream...that is how you open a file read from it into a string, then output to a file, this was compiled with GNU though...hope it still helps
big_int_calc.cpp
A program that will read in a large integer from a file then preform calculations on the number. It will then output the result into another file.
*/
#include%26lt;iostream%26gt;
#include%26lt;fstream%26gt;
#include"big_int.h"
#include"char_to_int.h"
int main(){
  //ask the user for options and store as an integer
  int option;
  std:: cout %26lt;%26lt; '\n'%26lt;%26lt; "Enter an option: " %26lt;%26lt; '\n';
  std::cout %26lt;%26lt;  "1. Read a big intger from file and add" %26lt;%26lt; '\n';
  std::cout %26lt;%26lt;  "2. Enter big integer,and multiply by 10^n" %26lt;%26lt; '\n';
  std::cout %26lt;%26lt;  "3. Multiply an integer read from file" %26lt;%26lt; '\n';
  std:: cin %26gt;%26gt; option;
  //big int to store result in
  big_int result;
  //big int to store input data
  big_int in_data1, in_data2;
  if(option == 1){
  //variable to store the file name
  char file_name[20];
  //ask the user for the name of the file they would like to read from
  std:: cout %26lt;%26lt; "Enter the file name you would like to read a big integer from: ";
  //input of the file name
  std:: cin %26gt;%26gt; file_name;
  //opens the file, if file is not opened error is output
  std::ifstream in;
  in.open(file_name);
  // outputs error if needed
  if(!in){
    std::cout%26lt;%26lt; "Error cannot open file" %26lt;%26lt; '\n';
  };
  
  //read the data from the file into a C-array that is a member of the class big_int
  
  int index = 0; // variable to try and make the array size itself
  bool second_num = false; // variable to declare when to start second number input
  int i = 0; //i is used as the index, but is also the size of the array for in_data1
  while(!in.eof()){
    char temp;
    in.get(temp); // stores the value of the char read in into temp
    if(temp != '\n' %26amp;%26amp; temp != '+'){ //filters new line char and +
   
      if(temp == ';'){ // checks for the second number
second_num = true; //sets the value of second num to true once ; is read
// the size of the first big int will be i -1
i = 0; // resets the index
      };
      //reads first number
      if(second_num == false){
in_data1.num_array[i] = char_to_int(temp); //stores value as integer in big_int data type
      ++i;
      in_data1.set_array_size(i); // sets the size of the array
      };
      //reads in second number with filters
      if(second_num == true %26amp;%26amp; temp != ';'){ //filters out ; that may be at the end of the file
in_data2.num_array[i] = char_to_int(temp); //stores the value as an integer in big_int
++i;
in_data2.set_array_size(i); // sets the value of the array size
      };    
    }; 
  };
  // variable that decides if user wants to output to screen or file  
  int result_option;
  if(option == 1){
    //calculates result
    result = in_data1 + in_data2;
    //ask user for the input of the option
  std::cout%26lt;%26lt; '\n' %26lt;%26lt; "To output result to screen enter 1, to output to file enter 2: ";
  std:: cin %26gt;%26gt; result_option;
  result = in_data1 + in_data2;
  if(result_option == 1){
    std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    result.output();
    std::cout %26lt;%26lt; '\n';
  };
  };
  
  // outputs to file if option is 2
  if(result_option == 2){
    //calculates result
    result = in_data1 + in_data2;
    //ask the user the name of file to write to
    std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
    char outfile_name[20];
    //declares the file to be written to
    std::cin %26gt;%26gt;outfile_name;
    std::ofstream outfile(outfile_name, std::ios::app);
    
    // format output
    outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    for(int i = 0; i %26lt; result.size(); ++i){
      outfile %26lt;%26lt; result.num_array[i];
      if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
      };
      
    };
    // output of ; at end of file
    outfile %26lt;%26lt; ';';
    
  };
  };
  // declares a big int entered int which is entered by the user
  big_int entered_int;
  if(option == 2){
    //ask the user what they would like to do with the result
    int output_option;
    std::cout %26lt;%26lt; "To output to screen enter 1, to output to file enter 2: ";
    std::cin %26gt;%26gt;output_option;
    char temp;
    char int_in[500];
    
    //inputs the big integer to the big_int class array
    int index = 0;
    std::cout %26lt;%26lt; "Enter big integer with e to end input: " %26lt;%26lt; '\n';
    //uses sential value to end input
    while(std::cin%26gt;%26gt; temp){
      if(temp == 'e'){
break;
      };
      //converts the char to integers
      entered_int.num_array[index] = char_to_int(temp);
      ++index;
  };
    //inputs the value of n
    entered_int.set_array_size(index);
    std::cout %26lt;%26lt; "Enter n (10^n) n=";
    int n;
    std::cin%26gt;%26gt; n;
    for(int i = 0; i %26lt; n; ++i){
      //adds 0's on the end of the array
      entered_int.num_array[entered_int.size()... = 0;
    };
    // sets new array size
    entered_int.set_array_size(entered_int.s... + n);
    if(output_option == 1){
    std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    entered_int.output();
    };
  
    if(output_option == 2){
      //writes to file if user chooses that optioin
    std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
    char outfile_name[20];
    std::cin %26gt;%26gt;outfile_name;
    std::ofstream outfile(outfile_name, std::ios::app);
    //formats output with 50 char per line
    outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    for(int i = 0; i %26lt; entered_int.size(); ++i){
      outfile %26lt;%26lt; entered_int.num_array[i];
      if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){
outfile %26lt;%26lt; '\n';
      };
      
    };
    //adds ; at end of file
    outfile %26lt;%26lt; ';';
    
    };
  };
  // variable that decides if user wants to output to screen or file  
  int result_option;
  if(option == 3){
  //variable to store the file name
  char file_name[20];
  //ask the user for the name of the file they would like to read from
  std:: cout %26lt;%26lt; "Enter the file name you would like to read a big integer from: ";
  //input of the file name
  std:: cin %26gt;%26gt; file_name;
  //opens the file, if file is not opened error is output
  std::ifstream in;
  in.open(file_name);
  // outputs error if needed
  if(!in){
    std::cout%26lt;%26lt; "Error cannot open file" %26lt;%26lt; '\n';
  };
  
  //read the data from the file into a C-array that is a member of the class big_int
  
  int index = 0; // variable to try and make the array size itself
  bool second_num = false; // variable to declare when to start second number input
  int i = 0; //i is used as the index, but is also the size of the array for in_data1
  while(!in.eof()){
    char temp;
    in.get(temp); // stores the value of the char read in into temp
    if(temp != '\n' %26amp;%26amp; temp != '+'){ //filters new line char and +
   
      if(temp == ';'){ // checks for the second number
second_num = true; //sets the value of second num to true once ; is read
// the size of the first big int will be i -1
i = 0; // resets the index
      };
      //reads first number
      if(second_num == false){
in_data1.num_array[i] = char_to_int(temp); //stores value as integer in big_int data type
      ++i;
      in_data1.set_array_size(i); // sets the size of the array
      };
      //reads in second number with filters
      if(second_num == true %26amp;%26amp; temp != ';'){ //filters out ; that may be at the end of the file
in_data2.num_array[i] = char_to_int(temp); //stores the value as an integer in big_int
++i;
in_data2.set_array_size(i); // sets the value of the array size
      };    
    }; 
  };
    //calculates result
    result = in_data1 * in_data2;
    //ask user for the input of the option
  std::cout%26lt;%26lt; '\n' %26lt;%26lt; "To output result to screen enter 1, to output to file enter 2: ";
  std:: cin %26gt;%26gt; result_option;
  if(result_option == 1){
    std::cout %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    result.output();
    std::cout %26lt;%26lt; '\n';
  };
  };
  
  // outputs to file if option is 2
  if(result_option == 2){
    //calculates result
    result = in_data1 + in_data2;
    //ask the user the name of file to write to
    std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
    char outfile_name[20];
    //declares the file to be written to
    std::cin %26gt;%26gt;outfile_name;
    std::ofstream outfile(outfile_name, std::ios::app);
    
    // format output
    outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    for(int i = 0; i %26lt; result.size(); ++i){
      outfile %26lt;%26lt; result.num_array[i];
      if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
      };
      
    };
    // output of ; at end of file
    outfile %26lt;%26lt; ';';
    
  };
  /*
  if(option == 4){
    std::cout %26lt;%26lt; "Input a number to calculate the factorial of: ";
    int fact_in;
    std::cin %26gt;%26gt; fact_in;
    big_int result;
    result = fact(fact_in);
    std::cout %26lt;%26lt; '\n' %26lt;%26lt; "Enter 1 to output to screen, enter 2 to output to file: " ;
    int result_option;
    std:: cin %26gt;%26gt; result_option;
    if(result_option == 1){
      result.output();
    };
  // outputs to file if option is 2
  if(result_option == 2){
    
    //ask the user the name of file to write to
    std::cout %26lt;%26lt; "Enter the file name to write to: " %26lt;%26lt; '\n';
    char outfile_name[20];
    //declares the file to be written to
    std::cin %26gt;%26gt;outfile_name;
    std::ofstream outfile(outfile_name, std::ios::app);
    
    // format output
    outfile %26lt;%26lt; "The result is: " %26lt;%26lt; '\n';
    for(int i = 0; i %26lt; result.size(); ++i){
      outfile %26lt;%26lt; result.num_array[i];
      if( i % 50 == 0 %26amp;%26amp; i %26gt; 0){ //starts new line every 50 char
outfile %26lt;%26lt; '\n';
      };
      
    };
    // output of ; at end of file
    outfile %26lt;%26lt; ';';
  };
  };
  */
 return 0;
};
Subscribe to:
Post Comments (Atom)
 
No comments:
Post a Comment