3
Good night.
I’m working on a project for the College in C where I have a data structure to work with the data common to "Offenders," or those who committed an infraction.
typedef struct Infractores
{
int ordemdeEntrada;//ordem de entrada da infração ... começa em 1 acaba em N
char *marca;
char *modelo;
char *matricula;
double valorportagem;
int classeVeiculo;
struct Infractores *seguinte;
};
If you’ve noticed I’m using pointers to *marca , *modelo
... and I have a struct Infractores *seguinte;
. This happens because of having to implement lists in the project.
I happen to want to implement a method of reading the latest offenders from a file, for example those who committed offences yesterday. To achieve this I also developed a method:
void ListaInfractoresAnteriores(Infractores *f)
{
const char *filenameinfractors = "C:/Users/Vitor/documents/visual studio 2013/Projects/AED II/Resolucao_Teste/Projecto/VVManager/lastdayinfractors.txt";
FILE *ficheiroInf = fopen(filenameinfractors, "r");
//struct TesteInfractores auxiliar;
struct Infractores *auxiliar;
auxiliar = f;
while (!feof(ficheiroInf))
{
if (fscanf(ficheiroInf, "%d %s %s %lf %d \n", &auxiliar->ordemdeEntrada, *auxiliar->marca, *auxiliar->modelo, &auxiliar->valorportagem, &auxiliar->classeVeiculo) != NULL)
{
printf("Marca %s",*auxiliar->marca);
}
}
}
In this method I try to test on what if the input of mine fscanf()
for <>
(different) from NULL
(Null), so he must write the mark, in this case the car, which committed the infringement.
I absolutely cannot read the file using this data structure. How can I read data from the file taking into account that did not want to use a new data structure? Do I have to create new variables?
Note: I intend to use the data structure to manipulate files and manipulate lists.
For what you want to do you need a data structure to read each line and store each line read in a different structure. Or create an array of structures. Second fscanf no.
– Rafael Bluhm
Hi Vitor - I know it is not your question - but as it is a real case of data manipulation (and not a school porblema, as most questions in C) - I suggest you address your problem in a higher-level language than C. This is a typical system where your data could be in an SQL (or Nosql) database-and if you use Python, Java, Ruby or Javascript - you’ll have far less Boilerplate to use SQL, and life will be much quieter
– jsbueno
If you want to go the way of trying to use another language there - write me that I can give a more extensive guidance (then we will document here in S.O. for the next ones if applicable). My email is in the profile.
– jsbueno
Sorry - now that I’ve seen that it’s also a job for college. So, you probably don’t have an option to change language - and you’ll have to learn a lot to do it in C - pay attention to @Anthonyacioly’s comment in Rafael’s answer below.
– jsbueno
I could even use a more advanced language, like java or C#, but the work is for Algorithms and it is recommended, here use structured languages, instead of object-oriented languages
– Vitor Ferreira
I think it is good to study more the basic concepts of the language (blurring pointers to structures, return of function of the standard library, use of typedef etc), has many silly errors in this code, mistakes that whoever is trying to implement something more complex as a data structure should not make.
– Rafael Bluhm