Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4622838
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:54:00+00:00 2026-05-22T02:54:00+00:00

I have two functions one that saves content of my array to a textfile

  • 0

I have two functions one that saves content of my array to a textfile and a function that reads the same into the array. Everything was written in VS and now I’m trying to make a GUI to my app.
My save to file function works like a charm but when I try to read that file in QT with my read function nothing happens. Do I have to rewrite the function for it to work in QT? if not what could be the problem?
What can i “throw” if in.fail()?

 void DH::read()
{

    ifstream in("text.txt");
    string strKcal=" ";
    int kcal=0;
    string strCarb=" ";
    double carb=0.0;
    string strProtein=" ";
    double protein=0.0;
    string strLipid=" ";
    double lipid=0.0;
    string name;
    string usrName;
    string usrName1;
    string usrName2;
    string date;
    string nrs;
    getline(in,nrs);
    this->nrOfDiets=atoi(nrs.c_str());


    if(!in.fail())
    {
        for(int i=0;i<this->nrOfDiets && in.good();i++)
        {
            getline(in,usrName1,' ');
            getline(in,usrName2);
            usrName=usrName1+ " " + usrName2;
            getline(in,date);
            getline(in,name,'\t');
            getline(in,strKcal,'\t');
            getline(in,strCarb,'\t');
            getline(in,strProtein,'\t');
            getline(in,strLipid);


            kcal=atoi(strKcal.c_str());
            carb=atoi(strCarb.c_str());
            protein=atoi(strProtein.c_str());
            lipid=atoi(strLipid.c_str());


            this->dh[i]=new Diet(name,kcal,protein,carb,lipid,usrName,date);

        }
    }
    else
        //cout<<"Error!"<<endl;

    in.close();
}

I know I could’ve used in>> instead of getline all the time, but for some reason it didn’t work :S

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:54:01+00:00Added an answer on May 22, 2026 at 2:54 am

    Generally, no, you shouldn’t need to do anything differently to make correct code work as part of a Qt application. I haven’t studied your code in detail, but it seems fine at a glance at least.

    However, there are some things that will be different. First of all, control in graphical programs is usually inside out from command line programs. You’re not going to be writing the program flow explicitly. Instead, you’ll be reacting to events (button clicks, menu selections, etc.). Second, you’re typically running them via some different launch method (clicking an icon or running it from the IDE) that can change things like the current working directory of the program.

    Without more information, we can’t tell you what your problem is. But I’d check that the program is actually running from a directory where the relative path “text.txt” refers to a valid file. Also, and this may sound crazy, but verify that your code is actually being called.

    On a side note, your last sentence about operator>> vs. getline…sometimes it’s fine to just move on to something that works, but you’d probably be well served to spend the time to figure things like this out when you can. Learning why something doesn’t do what you thought it would can often save you a huge amount of time and frustration later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions whose underlying logic is the same but in one case
I have a function which parses one string into two strings. In C# I
I have two functions that have different enough logic but pretty much the same
I have two functions, one which parses all the arguments sent to the function
My problem is that I have two functions and one of the functions calls
I have two PHP functions - one that builds a html form and another
So let's say I have two different functions. One is a part of the
Say I have the following code: function One() {} One.prototype.x = undefined; function Two()
I have two begininer programs, both using the 'while' function, one works correctly, and
I have a function which returns a one-sided intersection of values between two input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.