Thread: Altering I/O data from a text file

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    1

    Altering I/O data from a text file

    I'm looking for assistance on a program I'm making. Essentially I have to read in a set of points, alter them, and then print them out on a separate text file. Reading/Reprinting is no problem. I'm having trouble with the alterations.

    The first three sets of data from the text files are three separate points (x,y,z). What I need to do is subtract these three points from the next three points (x2, y2, z2) then subtract that value from the following set of points (x3, y3, z3) and so on down the chain. Think of it as moving from one point, to a new point, to a new point.

    I feel like I'm overlooking something obvious, but everything I've tried thus far hasn't worked out. Any help is appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_STRING_LENGTH 100
    
    
     /*
      *
      */
    
    
    typedef struct Vector Vector;
    struct Vector {
    	float x;
    	float y;
    	float z;
    };
    
    
    void getVectorFromString(char string[], Vector *vector);
    void writeVectorToFile(Vector vector, FILE *file);
    
    
    
    
    int main(int argc, char** argv) {
    
    
    	char currentLine[MAX_STRING_LENGTH];
    	int numLines = 0;
    
    
    	FILE* inputFile = fopen("points.txt", "r");
    	if (inputFile == NULL)
    	{
    		printf("File open failed\n");
    		return (EXIT_FAILURE);
    	}
    
    
    	while (!feof(inputFile))
    	{
    		fgets(currentLine, MAX_STRING_LENGTH, inputFile);
    		numLines++;
    	}
    
    
    	rewind(inputFile);
    
    
    	Vector* vectors = malloc(numLines * sizeof(Vector));
    	for (int i = 0; i < numLines; i++)
    	{
    		fgets(currentLine, MAX_STRING_LENGTH, inputFile);
    		getVectorFromString(currentLine, &vectors[i]);
    	}
    
    
    	fclose(inputFile);
    
    
    
    
    		FILE *outputFile = fopen("moveinfo.txt", "w");
    		if (outputFile == NULL)
    		{
    			printf("File open failed.\n");
    			return (EXIT_FAILURE);
    		}
    
    
    		for (int i = 0; i < numLines; i++)
    		{
    			writeVectorToFile(vectors[i], outputFile);
    		}
    
    
    		fclose(outputFile);
    
    
    
    
    	return (EXIT_SUCCESS);
    }
    
    
    
    
    
    
    void getVectorFromString(char string[], Vector *vector)
    {
    	int commaIndex = -1;
    	char *result = NULL;
    	result = strchr(string, ',');
    	char * stringStart = &string[0];
    	commaIndex = result - stringStart;
    
    
    	char* numberString = malloc((commaIndex + 1) * sizeof(char));
    	strncpy(numberString, string, commaIndex);
    	numberString[commaIndex] = '\0';
    	vector->x = atof(numberString);
    
    
    	string = &string[0] + commaIndex + 1;
    	result = strchr(string, ',');
    	stringStart = &string[0];
    	commaIndex = result - stringStart;
    
    
    	char* numberString2 = malloc((commaIndex + 1) * sizeof(char));
    	strncpy(numberString2, string, commaIndex);
    	numberString2[commaIndex] = '\0';
    	vector->y = atof(numberString2);
    
    
    	string = &string[0] + commaIndex + 1;
    
    
    	vector->z = atof(string);
    
    
    
    
    
    
    	free(numberString);
    	numberString = NULL;
    	free(numberString2);
    	numberString2 = NULL;
    
    
    }
    
    
    void writeVectorToFile(Vector vector, FILE *file)
    {
    	fprintf(file, "%.4f,%.4f,%.4f", vector.x, vector.y, vector.z);
    }

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Is the structure of your input text file?
    Code:
    x,y,z
    x,y,z
    ...
    x,y,z
    If so, you could read one line at a time and use a single sscanf(), per line, to obtain each x,y,z coordinates.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Altering / Recieving Information from text file question
    By hearthledger in forum C Programming
    Replies: 5
    Last Post: 12-02-2012, 07:26 PM
  2. Replies: 3
    Last Post: 05-25-2011, 05:54 PM
  3. Need help with altering info on a txt file
    By Netflyer in forum C Programming
    Replies: 29
    Last Post: 02-14-2008, 07:23 AM
  4. Replies: 1
    Last Post: 10-30-2002, 05:45 AM
  5. create a text file with data using text editor
    By fried egg in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 09:11 PM

Tags for this Thread