Code: Select all
#include
#include
#include
using namespace std;
string lines [100];
int used=0;
ofstream outfile("last100.txt", ios_base::out);
int main()
{
for (string line; getline(cin, line);)
{
if(used
Ich konnte keinen Fehler in meinem Code finden und selbst versuchte, es zu debuggen, schien nichts falsch zu sein. Die Variable "verwendet" nie über 100, wie es sollte. Programm, das in C geschrieben ist, also habe ich eine C -Version desselben Algorithmus erstellt. Hier sind die Teile, die dafür wichtig sind: < /p>
char lines[100][256];
int used = 0;
int main(int argc, char *argv[])
{
FILE *outfile = fopen("last100.txt", "w");
if (outfile == NULL)
{
printf("Error opening file for writing.\n");
return 1;
}
// other pieces of the program come here
if (used < 100) //again, add to the end if not full
{
snprintf (lines[used],256, // a bunch of integer variables come here//);
used++;
}
else //if full, shift lines one position back and add to the last position
{
for (int i = 0; i < 99; i++)
{
strcpy(lines[i], lines[i+1]);
}
snprintf (lines[99],256, // a bunch of integer variables come here//);
}
rewind(outfile); //seek to the beginning of the file
for (int i = 0; i < used; i++) //write to the file and flush
{
fprintf(outfile, "%s\n", lines[i]);
fflush(outfile);
}
}
Was passiert hier? Ångström-Verteilung V2018.12 "auf einer De10-Standard-Karte.>