Reputation: 1122
How to in C split file into two double arrays. I have X and Y positions save in file txt like:
X
3
5
7
12
Y
2
4
5
Actually I have code which find the line position of "Y", but I don't know how to save numbers after "Y".
while(fgets(temp, 512, plik) !=NULL) {
if((strstr(temp,"Y"))!=NULL) {
printf("A match found on line %d\n", line_num);
positionY = line_num;
printf("\n%s\n", temp);
find_result++;
}
line_num++;
}
if(find_result == 0) {
printf("dont find");
}
My second question is how leave the X and save numbers to the "Y" I have tabX and tabY to save the numbers and they are dynamically allocated.
Upvotes: 0
Views: 73
Reputation: 148880
Assuming tabY and tabY are already allocated with size tab_size
, you could do :
int afterX = 0;
int afterY = 0;
int val;
int *pX = tabX;
int *pY = tabY;
while(fgets(temp, 512, plik) !=NULL) {
if((strstr(temp,"X"))!=NULL) {
printf("A match for X found on line %d\n", line_num);
positionX = line_num;
printf("\n%s\n", temp);
find_result++;
afterY = 0;
afterX = 1;
}
else if((strstr(temp,"Y"))!=NULL) {
printf("A match found on line %d\n", line_num);
positionY = line_num;
printf("\n%s\n", temp);
find_result++;
afterY = 1;
afterX = 1;
}
else {
val = atoi(tmp);
if (afterY) {
if (pY - tabY < tab_size) {
*(pY++) = val;
}
else {
printf("More than %d values in Y\n", size);
}
}
else if (afterX) {
if (pX - tabX < tab_size) {
*(pX++) = val;
}
else {
printf("More than %d values in X\n", size);
}
}
}
line_num++;
}
if(find_result == 0) {
printf("dont find");
}
Upvotes: 0
Reputation: 1009
x.txt
X
1
2
3
Y
4
5
6
foo.cpp
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp = fopen("x.txt", "r");
int insertInX = 0, insertInY = 0;
char buf[512] = "";
if(!fp)
return -1;
while(fgets(buf, 511, fp) != NULL)
{
if(strncmp(buf, "X", 1) == 0)
{
insertInX = 1;
//insertInY = 0;
continue;
}
if(strncmp(buf, "Y", 1) == 0)
{
insertInY = 1;
//insertInX = 0;
continue;
}
if(insertInY)
{
//Add to Y
printf("In Y : %s\n", buf);
continue;
}
if(insertInX)
{
//Add to X
printf("In X : %s\n", buf);
//continue;
}
}
return 0;
}
output:
In X : 1
In X : 2
In X : 3
In Y : 4
In Y : 5
In Y : 6
Upvotes: 1
Reputation: 409166
You have your two arrays, X
and Y
. Also add another variable, a pointer that can point to either array.
Then when you see the "X"
in the input, you make the pointer point to the X
array, and if you see "Y"
in the input you make the pointer point to the Y
array. Then for all the numbers, just append it to the array pointed to by the pointer.
Remember that the pointer doesn't need to be saved, so you can increase the pointer every time you read a number to make it point to the next item in the array.
You should be careful so you don't write out of bounds of the arrays though, as that will lead to undefined behavior.
There are two ways of handling this:
One is to have another pointer, which you make point to one beyond the end of the array (e.g. by doing end_ptr = Y + size
), then make sure that the write-pointer never reaches the end-pointer.
Another way is to have another pointer that always points to the beginning of the array, and also the size of the array. The you can get the current index into the array by doing write_pointer - start_pointer
, and if the index is equal or larger than the size you are out of bounds.
Upvotes: 0