Solved

Reformat strings from .txt file

Posted on 1998-02-14
2
194 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

810 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