Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ read values wrong from txt files?

Tags:

c++

numbers

I have made a program that reads value from txt file "database.txt", but output is wrong when the number is three digit

ifstream myfile("database.txt");
int broj_rijeci = 0;
if (myfile.is_open())
{
    while (getline(myfile, line))
    {
        if (line.at(0) == '[')
        {
            int i = line.length() - 2;
            int brojac = 0;
            system("pause");
            while (line.at(i) != line.at(0))
            {
                input = line.at(i);
                ascii_convert(input);
                broj_rijeci = broj_rijeci + input * pow(10, brojac);
                i--;
                brojac++;
            }
        }
    }
    myfile.close();
}
else cout << "Unable to open file";

and my database looks like this:

[311]

output is "310"

like image 693
user3137147 Avatar asked Dec 26 '13 16:12

user3137147


People also ask

How to open and read a text file in C?

The algorithm for opening and reading a text file in C has given below: Assign file pointer to fopen () function with write format The above source code is simple to understand and friendly due to the use of multiple comment lines.

How do I fix data import from text/CSV error?

I'm experiencing issues with Data Import From Text/CSV where the import Wizard is incorrectly truncating columns and not importing all the data. To replicate: 1. In Excel select the "Data" menu from the ribbon 2. Click "From Text/CSV" 3. Browse and select the file to import (use a file with say 20 items per row, separated by your chosen delimiter.

How to read string from text file line by line in C?

This c program is used to read the text file line by line and we need to parse the format of each line in the file to required formats. fgets function is used to read string from text file till max characters in each line in file.

How to open and read a text file in Python?

Algorithm for Opening and Reading the text file 1 Start 2 Declare variables and file pointer 3 Open the file 4 If the file is not opened print error massage 5 Print the content of the text file using loop 6 Close the file 7 Stop


1 Answers

Since you haven't provided the implementation of ascii_convert it's difficult to tell if the problem is there or elsewhere. However you can greatly simplify the process and eliminate the need for your own conversion routines by using std::stringstream.

#include <sstream>

std::ifstream myfile("database.txt");
if (myfile.is_open())
{
    std::string line;
    while (std::getline(myfile, line))
    {
        if (line.size() > 1 && line[0] == '[' && line[line.size() - 1] == ']')
        {
            int value;
            if(std::istringstream(line.substr(1, line.size() - 2)) >> value)
            {
                // Conversion was a success ... do something with "value"
            }
            else
            {
                // Conversion failed. Handle error condition.
            }
        }
    }
    myfile.close();
}
else std::cout << "Unable to open file";
like image 78
Captain Obvlious Avatar answered Sep 28 '22 07:09

Captain Obvlious