Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Is my code correct for the file input.txt in c++ and java? how should I input from input.txt file?

+10 votes
asked Jul 21 by npatel300 (1,440 points)
// C++ version

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
   fstream newfile;
   newfile.open("input.txt",ios::out);  
   if(newfile.is_open())
      newfile<<"The string" << a = 1 << "is in the language.";
      newfile.close();
 
   newfile.open("input.txt",ios::in);
   if (newfile.is_open()){
      string ;
      while(getline(newfile)){
         cout << "The string"<< a = 1<< "is in the language." ;
      }
      newfile.close();
   }
}

// Java version

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main{
    public static void Main(String[] args)
    {
        File in = new File("input.txt");
        File out = new File("output.txt");
        
        try (Scanner scan = new Scanner(in);
            PrintWriter pw = new PrintWriter(new FileWriter(out, true)))
            {
                while (scan.hasNext ()){
                    pw.print1n(scan.next());
                }
            }
        catch (IOException e){
            e.printStackTrace();
            System.exit(0);
        }
    }
}

1 Answer

+1 vote
answered Jul 21 by Peter Minarik (32,160 points)
edited Jul 23 by Peter Minarik

You should compile your code and make sure it compiles at least so one doesn't have to chase problems like wrong naming.

I've fixed your code. See below.

C++

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    fstream newfile("input.txt", ios::out);
    if (newfile.is_open())
    {
        newfile << "The string is in the language.";
        newfile.close();
    }

    newfile.open("input.txt", ios::in);
    if (newfile.is_open())
    {
        string line;
        while (getline(newfile, line))
            cout << line;
        newfile.close();
    }
    return 0;
}

Java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        File in = new File("input.txt");
        File out = new File("output.txt");
        
        try (Scanner scan = new Scanner(in);
            PrintWriter pw = new PrintWriter(new FileWriter(out, true)))
        {
            while (scan.hasNext ())
                pw.println(scan.next());
        }
        catch (IOException e)
        {
            e.printStackTrace();
            System.exit(0);
        }
    }
}
commented Jul 23 by npatel300 (1,440 points)
Yes, I had compiled, but had few errors. Now I fixed it, but still when I run the code, I don't get any results either in java or c++. My question, how should I input these strings like a =1, a= a+b-c*d in input file to determine if these strings are in language or not? Do I have to include input.txt in main file or I create separate file named input.txt?
commented Jul 23 by Peter Minarik (32,160 points)
edited Jul 23 by Peter Minarik
Yes, you have to add a new file to your project and call it input.txt.

Maybe the below code helps more:

#include <iostream>
#include <fstream>
#include <string>

#include "Parser.h"

#define INPUT_FILE "input.txt"

static void CreateInput(const char * path)
{
    std::fstream file(path, std::ios::out);  
    if (file.is_open())
    {
        file << "three=1+2" << std::endl;
        file << "3=1+2" << std::endl;
        file << "squarearea=side**2" << std::endl;
        file.close();
    }
}

int main(int argc, char * argv[])
{
    CreateInput(INPUT_FILE);
    
    std::fstream file(INPUT_FILE, std::ios::in);
    if (!file.is_open())
    {
        std::cout << "Failed to read input file: " << INPUT_FILE << std::endl;
        return 1;
    }
    
    Parser parser;
    char sentence[255];
    while (!file.getline(sentence, sizeof(sentence)).eof())
    {
        if (parser.CanParse(sentence))
        {
            std::cout << "Sentence \"" << sentence << "\" is part of the language." << std::endl;
        }
        else
        {
            std::cout << "Sentence \"" << sentence << "\" is not part of the language. ";
            std::cout << "Parsing stopped at offset " << parser.CurrentOffset() << " on character '" << parser.CurrentChar() << "'." << std::endl;
        }
    }

    file.close();
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...