logo
down
shadow

Storing 4 values from each line from a txt file, into an object - C++


Storing 4 values from each line from a txt file, into an object - C++

By : J. Dor
Date : October 25 2020, 07:10 AM
may help you . You may want to organize your data structures to model the input data:
code :
struct Airport
{
  std::string    code;
  std::string    name;
  std::string    city;
  std::string    nation;
};
struct Airport
{
  //...
  friend std::istream& operator>>(std::istream& input, Airport& a);
};
std::istream& operator>>(std::istream& input, Airport& a)
{
    std::getline(input, a.code, ',');
    std::getline(input, a.name, ',');
    std::getline(input, a.city, ',');
    std::getline(input, a.nation);
    return input;
}
std::vector<Airport> database;
Airport a;
while (inputfile >> a)
{
  database.push_back(a);
}
bool Sort_By_Name_Ascending(const Airport& a, const Airport& b)
{
  return a.name < b.name;
}

//...
std::sort(database.begin(), database.end(), Sort_By_Name_Ascending);
std::istream& operator>>(std::istream& input, Airport& a)
{
    std::string record;
    std::getline(input, record);

    // Now extract the interesting fields from the string.
    std::istringstream record_stream;
    unsigned int record_number;
    char         comma;
    record_stream >> record_number;  // Read but ignore.
    record_stream >> comma;          // Read but ignore.
    std::getline(record_stream, a.code, ',');
    std::getline(record_stream, a.name, ',');
    std::getline(record_stream, a.city, ',');
    std::getline(record_stream, a.nation);
    return input;    
}


Share : facebook icon twitter icon
Reading a file line by line while storing an object in an array for each line

Reading a file line by line while storing an object in an array for each line


By : user337826
Date : March 29 2020, 07:55 AM
help you fix your problem I have a text file with a maximum of 4 lines to read from. Each line has a mixture of strings and integers spaced out by tabs. , Finally fixed my problem!
code :
public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException
{
    boolean found = false;
    int position = 0;
        if(canAddVehicle() == true)
        {
            for(int i = 0; i < vehicles.length && !found; i++)
            {
                if(vehicles[i] == null)
                {
                    position = i;
                    found = true;
                }
            }

               Scanner reader = new Scanner(file);
               while(reader.hasNext())
               {
                   Honda[position] = new Vehicle();
                   Honda[position].readRecord(reader);
                   vehicles[position] = Honda[position];
                   position++;

               }
                reader.close();
                return true;
        }
        return false;
}
Reading a text file line by line and storing an object in the array

Reading a text file line by line and storing an object in the array


By : Tamazapan
Date : March 29 2020, 07:55 AM
wish helps you The code I've currently created stores the first line of the text file, creates a new Vehicle object and puts it in the array at the first position of null, and stores the same line in every null value in the array. I need it to be able to: , Found my solution!
code :
public boolean addVehicle(Vehicle[] Honda) throws FileNotFoundException
{
    boolean found = false;
    int position = 0;
        if(canAddVehicle() == true)
        {
            for(int i = 0; i < vehicles.length && !found; i++)
            {
                if(vehicles[i] == null)
                {
                    position = i;
                    found = true;
                }
            }

               Scanner reader = new Scanner(file);
               while(reader.hasNext())
               {
                   Honda[position] = new Vehicle();
                   Honda[position].readRecord(reader);
                   vehicles[position] = Honda[position];
                   position++;

               }
                reader.close();
                return true;
        }
        return false;
}
Storing or not storing specific line in file on github

Storing or not storing specific line in file on github


By : Nishal Silva
Date : March 29 2020, 07:55 AM
This might help you I've figured out a way to not store the username in the bash script. Here's the bash solution I've implemented:
code :
# If an argument is passed, set as supercomputer username
if [ $# -eq 1 ]
then
  username=$1
# Else, use the current username as the supercomputer username
else
  username=$LOGNAME
fi
Splitting line from a file and storing as separate array values

Splitting line from a file and storing as separate array values


By : Sara Tong
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , This might not be the cause, but do not split by space. You may have tabs or LF characters, and/or leading and trailing spaces.
That is, do not use lines[x].split(" ");
code :
lines[x].split("[\\s]+"); //one or more spaces. 
Why isn't my object storing into an array each time I read a line of a file? Java

Why isn't my object storing into an array each time I read a line of a file? Java


By : Rioji
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm reading data in from a text file. Each line has a year at the beginning of the line followed by some data for each month. I'm trying to save both the year and month data into an object and storing it into an object array for each line I read. I'm pretty sure I've done everything right apart from my parseFile method. Here it is - , you place your object always at index 0:
code :
int i =0;
rainfallYears[i] = new RainfallYear(year,monthlyRain);
int i = 0;
while ((line1 = word_reader.readLine()) != null) {
Related Posts Related Posts :
  • What's wrong with these pointer initialization
  • Access Violation Using memcpy or Assignment to an Array in a Struct
  • Wrong operator() overload called
  • Subtractively sweep a solid through another in python (or C++)
  • boost ublas: rotate 2d vector
  • How can a Windows program temporarily change its time zone?
  • small & readable scheme interpreter in C++?
  • Cannot change the height of a combo box in the VS Dialog Editor
  • Access reading error when using class member variable
  • How to tell if two exe's are the same code-wise?
  • Modifying an old Windows program not to call exit after a keypress without source access
  • What is name lookup mechanism?
  • What does the C++ compiler error "looks like a function definition, but there is no parameter list;" mean?
  • Why does this crash with access violation to 0xcccccc...?
  • How to find out how namespace got polluted?
  • What is the _REENTRANT flag?
  • Managing many objects at once
  • How to create ActiveX DLL in Visual C++
  • Generic allocator class without variadic templates?
  • Comparation in JNI
  • Using a function with reference as a function with pointers?
  • How to initialize an unsigned long long type?
  • How to practically customize IE context menu?
  • Trying to Create a ToolBar with an ImageList, not working
  • How does the destructor know when to activate itself? Can it be relied upon?
  • Conway's Game of Life - C++ and Qt
  • Accessing any structs members at run-time
  • Including #includes in header file vs source file
  • How does switch compile in Visual C++ and how optimized and fast is it?
  • How to override nested C++ objects methods?
  • penalty for "inlined" classes
  • C++ Library for implementing a web services api over legacy code?
  • C++-Singleton class
  • Floating point precision in Visual C++
  • Compiler error when overriding virtual methods
  • What is the point of function pointers?
  • Which is faster in memory, ints or chars? And file-mapping or chunk reading?
  • ISO file in C++
  • Expected Class-name before { token
  • Can I trust floats or doubles representing integers to retain precision?
  • Qt execute multiple get with QNetworkAccessManager
  • Big and Little endian question
  • Visual Studio 2008 compiles anything in C++ file?
  • C++ pointer to functions, Beginner Question
  • Fastest algorithm for primality test
  • Passing array of pointers to another class
  • Handling macro redefinition without modifying .h files ... C / C++ language
  • Implement SSL with SSPI: How to start?
  • Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting)
  • C++ - Unicode Newline
  • Program crashing with 'std::out_of_range' error
  • Use signed or unsigned char in constructing CString?
  • Parallel execution policies in C++ 17
  • C++ Class Templates (Queue of a class)
  • Convert image into useable byte array in C?
  • pointer to preallocated memory as an input parameter and have the function fill it
  • Animated Image in Win32
  • error on compiling statically linked library created in Qt
  • How to easily pass a very long string to a worker process under Windows?
  • Can a class add friend classes at runtime in C++?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk