471,092 Members | 991 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

Please help me with C program!

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
0 961

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by da Vinci | last post: by
7 posts views Thread by tyler_durden | last post: by
reply views Thread by raa abdullah | last post: by
5 posts views Thread by mohammaditraders | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.