Originally Posted by
Salem
So your numbers are not actually separated by : then.
Code:
int lineNum, numChars;
if ( sscanf(buffer,"Linha%d:%n",&lineNum,&numChars) == 1 ) { // %n does NOT count as a conversion
char *p = buffer + numChars;
// now you can walk the line using a much simpler strtol loop
}
Finally working... Thanks a million.
Still going to need to read about strtol when i got some time.
I still didn't yet grasp the concept of it the endptr parameter.
strtol - C++ Reference.
Code:
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 100
int main(int argc, char** argv) {
FILE *pFile = fopen("ex1.txt", "rt");
char buffer[LENGTH];
int * vector;
size_t size;
if (pFile != NULL) {
fgets(buffer, sizeof (buffer), pFile);
sscanf(buffer, "%*s %d", &size); // %*s - Consume and throw away, and get the size of the matrix
vector = malloc(sizeof (int) * size);
int lineNum, numChars;
int value;
int count = 0;
while (fgets(buffer, sizeof (buffer), pFile)) {
if (sscanf(buffer, "Linha%d:%n", &lineNum, &numChars) == 1) { // %n does NOT count as a conversion, it counts the numbers of caracters https://stackoverflow.com/questions/3401156/what-is-the-use-of-the-n-format-specifier-in-c
char *p = buffer + numChars;
while ((value = strtol(p, &p, 0)) && errno == 0) {
vector[count] = value;
count++;
}
count = 0;
for (int i = 0; i < size - 1; i++) {
if (vector[i] == vector[i + 1])
printf("The numbers %d %d, are equals\n", vector[i], vector[i + 1]);
}
}
}
free(vector);
fclose(pFile);
return (EXIT_SUCCESS);
}
}