Warum gibt mein 2D-Vektor aus C-Strings das Element nicht am angegebenen Index aus?C++

Programme in C++. Entwicklerforum
Anonymous
 Warum gibt mein 2D-Vektor aus C-Strings das Element nicht am angegebenen Index aus?

Post by Anonymous »

Ich stecke seit Wochen in diesem Thema fest und verstehe es nicht. Ich denke, der einzige Weg, richtig zu zeigen, was passiert, besteht darin, das Ganze zu erklären.
Ziel
Ich erstelle ein Programm für ncurses, das es mir ermöglicht, Zeichen aus einer separaten Datei zu nehmen und sie an einer bestimmten Stelle auf dem Bildschirm einzuprägen. Ich bin gerade dabei, UTF-8-Zeichenunterstützung zu implementieren, damit die Textgrafik interessanter wird und viel mehr Zeichen als nur ASCII verfügbar sind.
Wichtige Erkenntnisse
Da ich macOS verwende, unterstützt ncurses breite Zeichen nicht direkt. Allerdings habe ich herausgefunden, dass ich addstr() verwenden kann, um breite Zeichen zu drucken. Aus diesem Grund habe ich das Programm so entworfen, dass es addstr() anstelle von addch() verwendet.
Das Programm
Dieses Programm besteht hauptsächlich aus einer Funktion, stamp(). Es handelt sich um eine Funktion, die ich für ncurses erstellt habe, um Textgrafiken einfacher auf dem Bildschirm zu platzieren. Dies erfolgt in mehreren Schritten:
  • Öffnen Sie die ausgewählte Textdatei.
  • Geben Sie eine Schleife ein, die jeweils eine Zeile der Datei liest.
  • Durchlaufen Sie die Bytes der Zeile.
  • Speichern Sie basierend auf dem ersten Byte ein bis vier Bytes von die Zeile in einen C-String mit dem Namen cStr.
  • Fügen Sie cStr sofort am Ende von cStrings hinzu, einem Vektor von const char *.
  • Gehen Sie zum nächsten Zeichen in der Zeile, indem Sie i basierend auf der Anzahl der gerade hinzugefügten Bytes erhöhen.
  • Wiederholen Sie diesen Vorgang, bis die gesamte Zeile gelesen und konvertiert ist.
  • Fügen Sie cStrings am Ende von cStringVectors hinzu, einem Vektor von Vektoren mit const char *.
  • Sobald die gesamte Datei in einen verwendbaren Zustand konvertiert ist, fahren Sie mit dem Stempelvorgang fort.
  • Der Stempelvorgang ist im Wesentlichen eine zweidimensionale Schleife, die jeweils ein cStr an addstr() übergibt und es auf dem Bildschirm platziert.
Code

Code: Select all

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

// the function requires the coordinates of the top left char in the file (x, y)
// then the length and height of the area in the file you want (length, height)
// then the coordinates you want the top left corner of the sprite to be placed on the screen (xpos, ypos)
// and finally the path to the file as a string (file)
void stamp(int x, int y, int length, int hight, int xpos, int ypos, string file) {
ifstream sprtfile(file);
string line;
move(ypos, xpos);

if (sprtfile.is_open()) {
// the file is read and then stores as a 3D vector
// each cstring is actually all the bytes required to be one utf-8 char
// so I can treat it as a 2D vector of chars
vector cStringVectors;

// this loop works one line of the file at a time
// and stores them in a 2d vector
// that stores the chars like cStringVectors except its only one line at a time instead of many
while (getline(sprtfile, line)) {
vector cStrings;

size_t i = 0;
int testvar = 0; //explained later

//this whole section gathers all the bytes of a utf-8 char into a cstring bundle called cStr
while (i < line.length()) {
unsigned char byte = static_cast(line[i]);

size_t charLength;
if ((byte & 0x80) == 0) {
charLength = 1; // 0xxxxxxx
} else if ((byte & 0xE0) == 0xC0) {
charLength = 2; // 110xxxxx
} else if ((byte & 0xF0) == 0xE0) {
charLength = 3; // 1110xxxx
} else if ((byte & 0xF8) == 0xF0) {
charLength = 4; // 11110xxx
} else {
charLength = 1; // Failsafe
}

// cStr works and it the proper char every time
string cStr = line.substr(i, charLength);

// in other examples you can use .push_back() to append vectors to vectors
cStrings.push_back(cStr.c_str());

// However, indexing cStrings to see whats in it have been the problem
addstr(cStrings[0]); // no matter what you set the index to, it doesn't seem to print that index
//ex: if you keep the index as 0 it should only ever print the first char in the line every how to time addstr() is called, instead every time you call it prints a new char

//the crazy part is if you set the index of cString to "testvar"
//it should have to print a new index each time,
//but instead it prints exactly the same thing as only using index zero!!!
testvar++;

refresh(); // ncurses function to display the new characters added to the screen
getch(); // pauses until it detects keyboard input so its easier to see each step playing out
i += charLength;  // moves on to the next char in the line from the file
}
cStringVectors.push_back(cStrings); // should add that line to the larger 3d vector
}
// loops that should make sure only the selected portion of the file is stamped on the screen
for (size_t i=y; i < y+hight; i++) {
for (size_t j=x; j < x+length; j++) {
move(ypos+i-y, xpos+j-x); // moves to the proper location to add the char
getch(); // pauses until it detects keyboard input so its easier to see each step playing out

// this one acts different
// no matter what i or j is, it only adds the last char in the file
addstr(cStringVectors[i][j]);
}
}
sprtfile.close();
} else {

cerr

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post