Solved

Reformat strings from .txt file

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

929 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

18 Experts available now in Live!

Get 1:1 Help Now