Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with reading in text file and adding new lines in file

Andrew EnglandAndrew England Member Posts: 2

I need to read in some text files with multiple numeric data columns, and whenever the value of the first column increases (ie, greater than the previous line), to add a black line separating these lines.
1.1
1.1
1.1
1.2
1.2
1.2
1.3

becomes:
1.1
1.1
1.1

1.2
1.2
1.2

1.3

int main() { int lines=0; char filename[100], ch; FILE * newfile; printf("enter file name: "); scanf("%s", filename); newfile = fopen(filename, "r+"); FILE * tempfile; tempfile =fopen("tempfile", "w"); while ((ch = fgetc(newfile)) !=EOF) { if (ch == '\n') { lines++; } } float *doub[lines]; char line[lines][100], rest[lines][100]; for (int i = 0; i < lines; i++) { fgets(line[i], 100, newfile); sscanf(line[i], "%f %s", doub[i], rest[i]); if ((i=0)) { fputs( line[i], tempfile); fputs( "\n", tempfile); } else if (i>0) { fputs(line[i], tempfile); fputs("\n", tempfile); if (doub[i] > doub[i-1]) { fputs( "\n", tempfile); } } } fclose(newfile); fclose(tempfile); return 0; }

This is what I have currently come up with, and it compiles, without errors on the couple of compilers that I have tried, though once i type the name of the text file I want sorted, it comes with an error:
segmentation fault (core dumped).
Any help and guidance would be greatly appreciated

Tagged:
Sign In or Register to comment.