Solved

Reformat strings from .txt file

Posted on 1998-02-14
2
192 Views
Last Modified: 2010-04-02
I need a way to reformat text from a text file: ie. if there are 2 paragraphs in the file that have 50 lines each, and each line contained 50 or more characters each; how do I reformat the text output to no more than 40 characters to each line, without chopping of entire words?
0
Comment
Question by:trying
2 Comments
 
LVL 1

Expert Comment

by:Nexial
ID: 1257262
Without actually writing the code I would use the following schema:

Use filelength to get the length of the file (as this is a small
file)
Allocate a buffer somewhat greater in size than this length.
Use the fgets function to read each successive line into the
buffer, where the start point of each buffer is the first location of the end_of_line character(s) on your system.   If you
have EOL = '\n' (which is usual), then each successive buffer will start at the '\n' location of the previous read.   This process will eliminate all EOL characters in the file except for the last one, which will be followed by a terminating '\0'

Once EOF has been reached on the input file, and the input file closed then:

Open the output file
Set initial_char_pointer = start_of_buffer
loop:
   Set end_char_pointer = initial_char_pointer+40
   Scan backwards using end_char_pointer for whitespace
   upon first whitespace found, replace whitespace with '\0'
   issue fputs from initial_char pointer.
   set initial_char_pointer to new end_char_pointer+1
   until all done.

Close the file.
           
0
 
LVL 2

Accepted Solution

by:
Ready4Dis earned 50 total points
ID: 1257263
#include <stdio.h>
#include <conio.h>

main ()
{
   FILE *in, *out;
   char word[40];
   int x, ctr, line;
   fopen("infile.txt","rb");
   fopen("outfile.txt","wb");
   while (!feof(in))
   {
     x++;
     line++;
     word[x]=getc(in);
     if (line>=40)
     {
       line=0;
       putc(13,out);
     }
     if (word[x]==32||word[x]=13)
     {
       memset(word,0,40);
       for (ctr=1;ctr<=x;ctr++)
            putc(word[x],out);
       x=0;
     }
   }
   return 0;
}
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now