-1

Like the title, I'm having some problems with my codes. It seems like the program doesn't stop. I think the problem is c++ couldn't read .json file because I've tested with text file, the program didn't make error, then I test with unexist file and the program loop like when I test with .json file. So I think c++ couldn't read unexist file and seem like .json have the same problem. Please help me. I just begin learnning c++ so if you can solve problem, I hope you can show me why my code is wrong and recommend the solutions. Here my code

 #include <iostream>
#include <fstream>
#include<string>
using namespace std;

void main()
{
    fstream file; 
    char a[80], b; 
    string c; 
    file.open("D://New folder//testcase//Project3//conf.json",ios::in ); 
    while (!file.eof())
    {
        file.getline(a, 80);

        cout << a << "\n";
    }
    if (file.fail()) cout << "Abc"; 
    file.close();  
    cin >> b; 
}

Here's my configuration file

{
  "name": "PF182-A01",
  "version": "1.0.0",
  "author": "Duc Dung Nguyen",
  "email": "nddung (at) hcmut.edu.vn",

  "WelcomeText": {
    "line1": "******************************************************************************",
    "line2": "*                  Welcome to CSE-HealthCare System                          *",
    "line3": "*     This is a simple application designed for the first assignment of PF   *",
    "line4": "* course (CO1011, Semester 182). The student must demonstrate the ability to *",
    "line5": "* write a program for a given problem. The student need to analyze the       *",
    "line6": "* requirements of the problem before implementing the application.           *",
    "line7": "******************************************************************************",
    "line8": "Email: nddung@hcmut.edu.vn",
    "line9": "(c) 2019 Duc Dung Nguyen All Rights Reserved."
  },
  "Menu": {
    "opt1": "Introduction",
    "opt2": "Login",
    "opt3": "Registration",
    "opt4": "Help",
    "opt5": "Exit"
  },
  "IntroTime": 3,
}
Hoang Son
  • 27
  • 5

1 Answers1

1

The file name is incorrect

file.open("D://New folder//testcase//Project3//conf.json",ios::in ); 

it should be

file.open("D:/New folder/testcase/Project3/conf.json",ios::in ); 

or

file.open("D:\\New folder\\testcase\\Project3\\conf.json",ios::in ); 

And the while loop is incorrect

while (!file.eof())
{
    file.getline(a, 80);

    cout << a << "\n";
}

it should be

while (file.getline(a, 80))
{
    cout << a << "\n";
}

See here Why is iostream::eof inside a loop condition considered wrong?

Finally it's always a good idea to check if a file open succeeded

file.open("D:/New folder/testcase/Project3/conf.json",ios::in ); 
if (!file.is_open())
{
    cout << "can't open file !!!\n";
    return 0;
}
john
  • 71,156
  • 4
  • 49
  • 68
  • Thanks for your help . I've edited my code but the program still didn't read correctly my confirugation file. I've add my configuration file to my post and I hope you can help me by solve the remain problem. – Hoang Son Feb 27 '19 at 10:44
  • @HoangSon What happens that is wrong and what did you expect to happen? – john Feb 27 '19 at 11:01