By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Please help me with C program!

P: 6
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");


if((fp=fopen(argv[1],"r+")) ==NULL)
printf("Cannot open file.\n");

printf("Enter ObjectID: ");
scanf("%s", str);

code = fgets(str2, 80, fp); /*reads 1st line and saves it in code*/
code2 = fgets(str3,80,fp); /*reads second line and saves it in code2*/
rewind(fp); /*begins at start of file*/
fprintf(fp,"OBJID,%s",code);/*writes first line*/

/*finishes first line*/

fprintf(fp,"%s,%s",str,code2); /*writes 2nd*/

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*/



return 0;

Jul 7 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.