Question: Stream I/O (working with strings)
Hi, I'm new to the Cprogramming board, and I'm hoping to get some help on a little project I am working on. I'm fairly new to programming as well, and I've been going through the K&R text along with the GNU C programming tutorial and some others.
While certain things have been, at times, frustrating and slow, I really feel like things are picking up, and I'm getting a better grasp on things. I would however, humbly request some help on a program I'm working on regarding string input/output on some files that I'm working with.
I'm a reporter/editor for an online news site, and I want to be able to take an unformatted text file (specifically an article) and apply certain formatting changes (to our site's specifications) to be able to save our editorial/production staff some time and from a whole lot of tedium. This includes formatting the date, author, source, etc. lines.
I have a rough idea of how I'll set up certain things so far, but I'm getting tripped up on some of the essentials, and it would help me a ton to get some feedback on it.
Below is what I have so far... I have to manually interrupt the program with Ctrl-c and it only copies 80% of the text, and I'm not sure why at this point:
**I'm sure I'm doing something embarrassingly wrong here, and I would very much appreciate it if someone could point that out; and I would love some feedback as far as the most effective way to deal with the input strings (eg. using control loops, etc.) in order to recognize what I need to so I can ouput the formatted text
Thanks so much!
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int c, i;
int bytes_read;
int nbytes = 100;
char *my_string;
FILE *stream_f1;
FILE *stream_f2;
stream_f1 = fopen ("formatted_sub.txt", "w");
stream_f2 = fopen ("sub.txt", "r");
while (*my_string != EOF) {
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_string, &nbytes, stream_f2);
fprintf (stream_f1, my_string);
}
fclose (stream_f1);
fclose (stream_f2);
return 0;
}