Question

I have a text file containing the following information: # Map # Small islands with treasure...

I have a text file containing the following information:

# Map

# Small islands with treasure in the NW corner and start in the SE corner.

M

5

o$...

oo...

#..o.

..oo.

....@

I want to read in the file but want to ignore the comments at the top (they begin with #). There can be infinite amount of comments. I want to store the letter (M) in a char variable, and the number as the map size. The following characters represent a treasure map, and I want to read that into a vector of vectors. How should the code look to accomplish this? I am writing the code in C++.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

There is only one assumption I am making: (Any number of) Comments will appear only in the top. When first character M appears there will be no comments after that.

  • Below is the implementation in C++:

#include<bits/stdc++.h>
using namespace std;

int main(){
   ifstream fread;
   fread.open("input.txt"); //Put the exact location of file to open in quoted text.
   string line;
   string arr[1000];       // Assign the array of size of appropriate length othewise data overflow may occur.
   int i=0;
   bool comments=true;       //This boolean is used to indicate that there are still comments in the text being read.
   while(fread.is_open() && getline(fread,line)){
       //Ignore the comments at the top, they start with the symbol '#'.
       if(line[0]=='#' && comments==true){
           continue;
       }
       else{
       comments=false;
       arr[i++]=line;

       }
   }
   fread.close();


   //Reading character M
   char M=arr[0][0];
   cout<<M;

   //Reading next integer size: size of map. stringstream object is used to cast string object to integer.
   //Note: If you are using cpp11 and higher you can use stoi() or atoi() directly for string to int conversion.
   stringstream obj(arr[1]);
   int size;
   obj>>size; //stringstream casts string to integer.
   cout<<"Size of the map: "<<size;

   //reading the map from arr.
   vector<vector<char> > Map(size);
   for(int j=0;j<size;j++){
       Map[j]=vector<char>(size);
       for(int k=0;k<size;k++)
           Map[j][k]=arr[j+2][k];
   }
  
   cout<<"\n\nThe map is:\n";
   for(int j=0;j<size;j++)
   {
       for(int k=0;k<size;k++)
           cout<<Map[j][k];
       cout<<endl;
   }
  

   //So the M variable,size variable and Map variable are the required data.
   return 0;
}

  • Below is the screenshot of desired output:


Thank You, hope that helps!

Add a comment
Know the answer?
Add Answer to:
I have a text file containing the following information: # Map # Small islands with treasure...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The program is done in C. This program opens a file containing binary or text and...

    The program is done in C. This program opens a file containing binary or text and reads every byte in the file and writes both the ASCII hex value for that byte as well as it’s printable (human-readable) character (characters, digits, symbols) to standard output. The issue I am having is when the file has multiple lines being read. The first line is read and done properly but the other lines do not work correctly. For instance, the text file...

  • . PART 3 – Output matrix data to text files using low-level File I/O What happens...

    . PART 3 – Output matrix data to text files using low-level File I/O What happens when you try to use the dlmwrite function with a delimiter that is more than one character? For example, say we require that the file must have two pipe symbols (I 1) between each value? For this part, you will need to output a magic square to a text file, but you will need to use low-level File I/O functions so that you can...

  • How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c;...

    How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c; int count = 0; for(i = 0; i < a; i++){ for(j = 0;j < 9;j++){ c = fgetc(fp); if(c == '-'){ board[i][j] = 0; } else if(c >= 1 && c <= 9) printf(" %d"); else return -2; } count++; } if(count != a-1) return -1; else return 0; }12 Read...

  • I wrote code in C++ that takes a text file containing information on different football players...

    I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...

  • This is for C++ Write a program that reads in a sequence of characters entered by...

    This is for C++ Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along with the number of times it occured. All non-alphabetic characters must...

  • Need this in C The starter code is long, if you know how to do it...

    Need this in C The starter code is long, if you know how to do it in other way please do. Do the best you can please. Here's the starter code: // ----------------------------------------------------------------------- // monsterdb.c // ----------------------------------------------------------------------- #include #include #include // ----------------------------------------------------------------------- // Some defines #define NAME_MAX 64 #define BUFFER_MAX 256 // ----------------------------------------------------------------------- // Structs typedef struct { char name[NAME_MAX]; int hp; int attackPower; int armor; } Character; typedef struct { int size; Character *list; } CharacterContainer; // ----------------------------------------------------------------------- //...

  • For the following task, I have written code in C and need help in determining the...

    For the following task, I have written code in C and need help in determining the cause(s) of a segmentation fault which occurs when run. **It prints the message on line 47 "printf("Reading the input file and writing data to output file simultaneously..."); then results in a segmentation fault (core dumped) I am using mobaXterm v11.0 (GNU nano 2.0.9) CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line...

  • CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods...

    CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...

  • Background: The first step towards helping someone 'decode' a message is often to count how many...

    Background: The first step towards helping someone 'decode' a message is often to count how many times each letter of the alphabet appears in the message. Then divide each count by the total number of letters to compute the relative 'frequency'.   From these frequencies, it may be easier to recognize which letters are assigned to the vowels. For your own reference, here is a table of standard letter frequencies from typical English text. (You do NOT need to display this...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT