I'm trying to read from a text file and have any multiple whitespaces between characters become one whitespace. If this how I do it using an array,

#include <stdio.h>

//Function prototype
void modifyspace(char *dest, char *src);

int main()
{
   /* Initialize an array of 2000 characters */
   char src[2000] = "there are       many     space    in    this line";

   /* Initialize dest array which */
   char dest[strlen(src) + 1];

   /* Calls the modifyspace function. */
   modifyspace(dest, src);

   /* Displays a blank space between the output and everything else. */
   printf(" \n");

   /* Display characters currently in the src array of characters */
   printf("%s\n", src);

   /* Displays the same src array of characters but each word only has one space
   between them */
   printf("%s\n", dest);

   return 0;
}

how can I do it reading from the file?

#include <stdio.h>

int main()
{
   FILE *fp;
   int i;

   fp = fopen("singlespace.txt", "r");

   if(fp == NULL) printf("No such file exists \n");
   else{
      do {
         i = getc(fp);
         putchar(i);
        }while (i != EOF);
   }

   fclose(fp);

   /* Displays a blank space between the output and everything else. */
   printf(" \n");

   return 0;
}

So what doesn't work? You need to give us informations if you need help.

So what doesn't work? You need to give us informations if you need help.

how can I do it reading from the file?

It's clear what he wants to achieve. Going by his code sample it's also clear what isn't working. But I know you have a reading disability so I'll not bully you with it. ;)

@izic

The problem you're having is that you made a function, modifyspace, which wants a destination and a source array but with the current setup you read one character at a time from the file. In order to use your function (doing a basic assumption on how it works) you would have to store the characters you read into an array first. Like your first example this imposes a limit on the input and isn't really error-prone.

A better solution would be to process characters as you read them so that once you're reading a next one the previous one(s) can be discarded as they are already discarded, written to file, printed to stdout or whatever you want to do with it.

This is likely a pretty easy thing to do as your 'modifyspace' function contains the algorithm for this already. Instead of looping over an array and accessing each element you use the character read from the filestream.

Let us know how it goes. Post the code you developed if you're running into issues.

So

void modifyspace(char *dest, char *src)
{
   for( ; *src; dest++, src++)
   {
      *dest = *src;

      if(isspace(*src))

            while(isspace(*(src+1)))
               src++;
   }
}

should be?

void modifyspace(char *fp)
{
   for( ; *fp; fp++)
   {
      if(isspace(*fp))

            while(isspace(*(fp+1)))
               fp++;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.