This program is supose to open a csv file, read it and add a column to it without changing the other columns. From the 2nd row to the last one the column will have the same value that the person puts called Object ID.
I already added the 1st column and the 2nd one but i need something that returns to the begging of the line. Since get fgets() function reads it then when i want to write it, the line its written on the line next to it. Heres what I have so far. I think I only need a way for it to return to the beggining of the line.
#include <stdio.h>
#include <stdlib.h>
#include "fitsio.h"
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
char str[80];
char str2[80];
char str3[80];
char str4[80];
char *code;
char *code2;
char *code3;
if (argc!=2 )
{
printf("Usage: programname inputfile \n");
printf("\n");
return(0);
}
if((fp=fopen(ar gv[1],"r+")) ==NULL)
{
printf("Cannot open file.\n");
exit(1);
}
printf("Enter ObjectID: ");
scanf("%s", str);
code = fgets(str2, 80, fp); /*reads 1st line and saves it in code*/
code2 = fgets(str3,80,f p); /*reads second line and saves it in code2*/
rewind(fp); /*begins at start of file*/
fprintf(fp,"OBJ ID,%s",code);/*writes first line*/
/*finishes first line*/
fprintf(fp,"%s, %s",str,code2 ); /*writes 2nd*/
while(!feof(fp) )
{
code3 = fgets(str3, 80, fp); /*reads line*/
/*NEED SOMETHING HERE that returns at the beginning of the line*/
fprintf(fp,"%s, %s",str,code3 ); /*writes line*/
}
fclose(fp);
return 0;
}
I already added the 1st column and the 2nd one but i need something that returns to the begging of the line. Since get fgets() function reads it then when i want to write it, the line its written on the line next to it. Heres what I have so far. I think I only need a way for it to return to the beggining of the line.
#include <stdio.h>
#include <stdlib.h>
#include "fitsio.h"
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
char str[80];
char str2[80];
char str3[80];
char str4[80];
char *code;
char *code2;
char *code3;
if (argc!=2 )
{
printf("Usage: programname inputfile \n");
printf("\n");
return(0);
}
if((fp=fopen(ar gv[1],"r+")) ==NULL)
{
printf("Cannot open file.\n");
exit(1);
}
printf("Enter ObjectID: ");
scanf("%s", str);
code = fgets(str2, 80, fp); /*reads 1st line and saves it in code*/
code2 = fgets(str3,80,f p); /*reads second line and saves it in code2*/
rewind(fp); /*begins at start of file*/
fprintf(fp,"OBJ ID,%s",code);/*writes first line*/
/*finishes first line*/
fprintf(fp,"%s, %s",str,code2 ); /*writes 2nd*/
while(!feof(fp) )
{
code3 = fgets(str3, 80, fp); /*reads line*/
/*NEED SOMETHING HERE that returns at the beginning of the line*/
fprintf(fp,"%s, %s",str,code3 ); /*writes line*/
}
fclose(fp);
return 0;
}