0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#include <crtdbg.h>  // needed to check for memory leaks (Windows only!)
#endif

#define FLUSH while(getchar() != '\n')

// Prototype Declarations
int readFile(FILE* ifp,char** words);

int main (void)
{
//  Local Definitions
FILE *ifp;
FILE *ofp;
char fnamer[100]="";
char **words;
int *freq;
int i;
int numWords =0;

//  Statements

    words = (char**)calloc (1001, sizeof(int));
        if( words == NULL )
        {
            printf("Error with Calloc\n");
            exit(111);
        }


  if (!(ifp=fopen("/Users/r3spectak/Desktop/song_row.txt", "r")))
  {
      printf("sucks");
      exit(100);
  }

    numWords = readFile(ifp,words);

    printf("%d", numWords);

    for(i=0;i<numWords;i++)
    printf("\n%s",words[i]);

    #ifdef _MSC_VER
    printf( _CrtDumpMemoryLeaks() ? "Memory Leak\n" : "No Memory Leak\n");
    #endif
    printf("\n\t\tEnd of Program\n");
    printf("\n\t\tHave a great day!\n");
   return 0;

}


/*===============readFile=================
Pre:
Post:
This function
*/

int readFile(FILE* ifp,char** words)
{

// Local Variables
char buffer[1000] = " ";
int numWords = 0;

// Statements
while (fscanf(ifp," %s",buffer)!=EOF)
    {
    words[numWords] = (char*)calloc(strlen(buffer)+1,sizeof(char));
                if( words[numWords] == NULL)
                {
                    printf("\n");
                    exit(111);
                }
                strcpy(words[numWords],buffer);
                numWords++ ;
    }

return numWords;

}

The input file contains the following : Row, row, row your boat, Gently down the stream. Merrily, merrily, merrily, merrily, Life is but a dream.

After fscanf my array prints

  Row,
    row,
    row
    your
    boat, and so on

what i want is,

Row
row
row
your
boat

i've tried %[^,.\n] and its not working for me. it prints rubbish

Kexy Kathe
  • 111
  • 1
  • 5
  • 11
  • So are you trying to comma-separate the input string? –  Mar 02 '13 at 06:22
  • You don't need to cast the return value of `calloc()` in a C program. – Carl Norum Mar 02 '13 at 06:22
  • @H2CO3 im trying not to read the commas at all. – Kexy Kathe Mar 02 '13 at 06:25
  • @CarlNorum i dont understand what you mean. – Kexy Kathe Mar 02 '13 at 06:25
  • @KexyKathe Carl Norum means [this.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) –  Mar 02 '13 at 06:26
  • @KexyKathe So you want an array of strings from the original string, with boundaries specified by the commas, or you just want to eliminate the commas from the string? –  Mar 02 '13 at 06:27
  • @H2CO3 read each word into the array omitting the commas. eliminate the commas and fullstops. if i were to use malloc all the other elements wont be set to null right? – Kexy Kathe Mar 02 '13 at 06:30
  • @KexyKathe Your second question makes no sense, but anyway... Why don't you copy over all characters to the other string *except commas and dots?* –  Mar 02 '13 at 06:38
  • @H2CO3 how exactly am i supposed to do that? im super lost. – Kexy Kathe Mar 02 '13 at 06:40
  • @KexyKathe `while (*original) { if (*original != ',') { *copy++ = *original++; } }` –  Mar 02 '13 at 06:43
  • H2CO3's answer will work (it should be an answer) or you could look up strtok(). I http://www.cplusplus.com/reference/cstring/strtok/ – c.fogelklou Mar 02 '13 at 06:50

1 Answers1

0

You might find this function especially useful. It will split your string into tokens, like the C equivalent of split() or explode().

Example:

#include <stdio.h>
#include <string.h>

int main (){
  char str[] ="Row, row, row your boat, Gently down the stream.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.");
  while (pch != NULL){
     printf ("%s\n",pch);
     pch = strtok (NULL, " ,.");
  }
  return 0;
}

I basically copied the man page example. Calling the function again with the first argument as NULL will bring up the next string token.

dudeofea
  • 330
  • 4
  • 21