05-29-2020 03:16 AM
Hello,
I made code which writes in txt file my array, but when I want to read file I get this error:
FATAL RUN-TIME ERROR: "Programa.c", line 99, col 21, thread id 5232: Argument must be an open stream.
My code:
#include <formatio.h>
#include <utility.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include "Programa.h"
// Globalus kintamieji
static int panelHandle, panelHandle2, panelHandle3;
FILE *fp; // deklaruojamas failo pointeris
int kiek = 0;
// Funkcijos
void Registravimo_funkcija(void);
void Antrastes_kurimas(void);
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "Programa.uir", PANEL)) < 0)
return -1;
if ((panelHandle2 = LoadPanel (0, "Programa.uir", PANEL_2)) < 0)
return -1;
if ((panelHandle3 = LoadPanel (0, "Programa.uir", PANEL_3)) < 0)
return -1;
SetActivePanel (panelHandle); // Nustatomas pagrindinis langas kaip active
DisplayPanel (panelHandle); // Isvedamas interface 1
RunUserInterface ();
DiscardPanel (panelHandle);
DiscardPanel (panelHandle2);
DiscardPanel (panelHandle3);
return 0;
}
int CVICALLBACK Registravimo_pradzia (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
DisplayPanel (panelHandle2); // Isvedamas darbuotojo registravimo langas
break;
}
return 0;
}
int CVICALLBACK Registravimo_pabaiga (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
Registravimo_funkcija();
SetActivePanel (panelHandle); // Nustatomas pagrindinis langas kaip active
HidePanel (panelHandle2); // Uzdaromas darbuotojo registravimo langas
//Registravimo_funkcija(); // Funkcija sukurianti nauja vartotoja
break;
}
return 0;
}
int CVICALLBACK Exit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
int CVICALLBACK Vartotojo_sasaja (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
DisplayPanel (panelHandle3);
fp = OpenFile ("filename.txt", VAL_READ_WRITE, VAL_APPEND, VAL_ASCII);
char ReadArray[300];
//ReadFile (fp, ReadArray, 200);
FileToArray ("filename.txt", ReadArray, VAL_CHAR, 300, 3, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_ROWS, VAL_ASCII);
/*int c;
if (fp) {
while ((c = getc(fp)) != EOF)
putchar(c)*/
fclose (fp);
/*
char line [256];
//fp = fopen ("TextFile.txt", "rt");
while (fgets (line, 256, fp))
InsertTextBoxLine(panelHandle3, PANEL_3_TEXTBOX, -1, line);
*/
//FileToArray ("filename.txt", ReadArray, VAL_CHAR, 300, 1, VAL_GROUPS_TOGETHER, VAL_GROUPS_AS_ROWS, VAL_ASCII);
//ReadLine (fp, ReadArray, 200);
//InsertTextBoxLine(panelHandle3,PANEL_3_TEXTBOX,-1,ReadArray);
break;
}
return 0;
}
int CVICALLBACK Isjungti_darbuotoju_stebejima (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetActivePanel (panelHandle); // Nustatomas pagrindinis langas kaip active
HidePanel (panelHandle3); // Uzdaromas darbuotojo registravimo langas
break;
}
return 0;
}
void Registravimo_funkcija(void)
{
//** Registravimo funkcijos kintamieji
char IDmasyvas[9]; // ID masyvas
char NAMEmasyvas[17]; // Vardo masyvas
char SURNAMEmasyvas[17]; // Pavardes masyvas
char POSITIONmasyvas[19]; // Pareigu masyvas
char WORKINGOURSmasyvas[17]; // Darbo valandu masyvas
//char SENDarray[81]; // Vartotojo masyvas
char Filearray[200];
int menuo, diena, metai; // Isiregistravimo i darba datos kintamieji
int itemIndex; // Ring kintamuju indekso kintamasis
//** Reiksmiu nuskaitymas
GetCtrlVal (panelHandle2, PANEL_2_ID, IDmasyvas); // Nuskaitomas ID
GetCtrlVal (panelHandle2, PANEL_2_NAME, NAMEmasyvas); // Nuskaitomas Vardas
GetCtrlVal (panelHandle2, PANEL_2_SURNAME, SURNAMEmasyvas); // Nuskaitoma Pavardė
//** Ring reiksmiu nuskaitymas
GetCtrlIndex (panelHandle2, PANEL_2_POSITION, &itemIndex); // Skiriamu pareigu kintamojo indekso nuskaitymas
GetLabelFromIndex (panelHandle2, PANEL_2_POSITION, itemIndex, POSITIONmasyvas); // Nuskaitomos Skiriamos pareigos
GetCtrlIndex (panelHandle2, PANEL_2_WORKINGOURS, &itemIndex); // Darbo valandu kintamojo indekso nuskaitymas
GetLabelFromIndex (panelHandle2, PANEL_2_WORKINGOURS, itemIndex, WORKINGOURSmasyvas); // Nuskaitomos Darbo valandos
//** I sukurta masyva itraukiama raide R, kad procesorius suprastu, kad norime sukurti nauja vartotoja
//sprintf(SENDarray,"R%15s%16s%16s%16s%16s",IDmasyvas, NAMEmasyvas, SURNAMEmasyvas, POSITIONmasyvas,WORKINGOURSmasyvas);
//** Siuntimas i MCU
//** Duomenu irasymas i .txt faila
fp = OpenFile ("filename.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII); // Atidaromas failas
if (kiek == 0)
{
Antrastes_kurimas();
}
kiek = kiek + 1;
sprintf(Filearray, " %3d %8s %13s %15s %19s %13s", kiek, IDmasyvas, NAMEmasyvas, SURNAMEmasyvas, POSITIONmasyvas, WORKINGOURSmasyvas);
//InsertTextBoxLine(panelHandle,PANEL_TEXTBOX,-1,Filearray);
WriteLine (fp, Filearray, -1);
CloseFile (fp);
}
void Antrastes_kurimas(void)
{
// Masyvas antrastes
char Masyvas[200] = {" # ID Vardas Pavardė Pareigos Darbo valandos Išdirbta valandų Pavėluota į darbą Išeita per anksti "};
WriteLine (fp, Masyvas, -1); // Rasomas masyvas i faila
}
Solved! Go to Solution.
05-29-2020 06:09 AM
Hi, there is a lot to say about your code!
Let's start from Registravimo_funkcija () function that writes the fie. You declare a global file handle of type FILE * but you are using functions from the Formatting and I/O library that expect a file handle of type int (as you can read in the function help!). You should have found several errors and warnings in compilation, I tend to say that this code could hardly run and if run would probably generate garbage if not nothing at all. But since you are generating as ASCII file you can examine it with notepad and see the result. As a last hint, there is no need to use a global variable for that since you do not need to share the file handle across multiple functions.
Coming to Vartotojo_sasaja () function tat should read the file, FileToArray () function does not need the file to be open before: it opens and closes the file by itself. Furthermore, the function expects the file contains data of the same type, but you are writing lines mixed with numerics and texts so the function cannot interpret it correctly.
Finally, you haven't put any error checking in your code! I strongly suggest you to implement a robust error checking method in all functions that perform I/O operations so that every abnormal situation found will be detected for you to correct it.