Reading in words from file[C]

Help With reading in from file

Page 1 of 1

9 Replies - 1321 Views - Last Post: 24 September 2010 - 06:34 PM Rate Topic: -----

#1 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Reading in words from file[C]

Posted 24 September 2010 - 05:30 PM

Okay so I have to read in from a file called story and save each word in a spot in the array. I thought I had an Idea of how to do that but I am getting Unexpected output.

Here is my code

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

#define SIZEWORD 1000
#define SIZECHAR 80

int main(void)
{
  //Local Declarations
  FILE* fh;
  char word[SIZEWORD];
  int timesinfile[SIZEWORD],n;


  //Open file
  if(!(fh = fopen("story","r")))
  {
    printf("ERROR");

    exit(100);
  }
  fh = fopen("story","r");
  //Putting in string

  for(n = 0;n < 4;n++)
  {
    fscanf(fh,"%s",word[n]);
    printf("%s",word[n]);
    }
   fclose(fh);
return(0);
}



The file story contains "This is a test to see if I can read in words"
I am getting (null) (null) (null) (null) when i need it to say This is a test

Eventually I will change it so it reads in until the end of file but for now I can't even get the first words.

Any Help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading in words from file[C]

#2 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: Reading in words from file[C]

Posted 24 September 2010 - 05:41 PM

Can you answer these questions:

What type is the variable word?

Given that answer what type is the variable word[n]?

What type is fscanf expecting if you have passed it %s?
Was This Post Helpful? 0
  • +
  • -

#3 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Reading in words from file[C]

Posted 24 September 2010 - 05:49 PM

View PostBanfa, on 24 September 2010 - 04:41 PM, said:

Can you answer these questions:

What type is the variable word?

Given that answer what type is the variable word[n]?

What type is fscanf expecting if you have passed it %s?


The type for word is Char..I was hoping to store Whole words in each part of the array. I am guessing that It has to do with Pointer location. I need each part of the array to point to a word.

So does that mean I need to change it to
char* word
? or something along the lines of this?
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading in words from file[C]

Posted 24 September 2010 - 05:56 PM

word[] is an array of characters.
You seem to want an array of strings (character arrays)

If that's what you want that's what you will have to declare.
Was This Post Helpful? 1
  • +
  • -

#5 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Reading in words from file[C]

Posted 24 September 2010 - 06:03 PM

View Postjanotte, on 24 September 2010 - 04:56 PM, said:

word[] is an array of characters.
You seem to want an array of strings (character arrays)

If that's what you want that's what you will have to declare.


Is there a way to declare that in C? I don't recall a string data type in C...just char
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading in words from file[C]

Posted 24 September 2010 - 06:07 PM

View PostMcSick, on 25 September 2010 - 10:03 AM, said:

Is there a way to declare that in C?

Yep sure is.
Check your text book for array of arrays.

View PostMcSick, on 25 September 2010 - 10:03 AM, said:

I don't recall a string data type in C...just char

Correct.
A string in C is a character array.
That's what the bit in parens in my post is saying.
word is a character array which is a C string.
Was This Post Helpful? 0
  • +
  • -

#7 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Reading in words from file[C]

Posted 24 September 2010 - 06:17 PM

View Postjanotte, on 24 September 2010 - 05:07 PM, said:

View PostMcSick, on 25 September 2010 - 10:03 AM, said:

Is there a way to declare that in C?

Yep sure is.
Check your text book for array of arrays.

View PostMcSick, on 25 September 2010 - 10:03 AM, said:

I don't recall a string data type in C...just char

Correct.
A string in C is a character array.
That's what the bit in parens in my post is saying.
word is a character array which is a C string.


I am looking in my book, the problem is in the book it is unclear. They don't scan words in they just declare them and use pointer manipulation (not my strong suit) I tried copying what they did sort of but still I am just getting (null) (null) (null) (null)

My code revised
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZEWORD 1000
#define SIZECHAR 80

int main(void)
{
  //Local Declarations
  FILE* fh;
  char* word[SIZEWORD];
  char** pstring=0;
  int timesinfile[SIZEWORD],n;


  //Open file
/*  if(!(fh = fopen("story","r")))
  {
    printf("ERROR");

    exit(100);
  }*/
  fh = fopen("story","r");
  //Putting in string
  printf("%d",feof(fh));
  for(n = 0;n < 4;n++)
  {
    fscanf(fh,"%s",word[n]);
    printf("%s",*word);
    }
   fclose(fh);
return(0);
}


Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading in words from file[C]

Posted 24 September 2010 - 06:30 PM

You seem to just be guessing now.
That might work but the odds are against you.

Add comments to your code (especially those lines inside the for loop).
What is each line doing?
Is that what it should do?
Was This Post Helpful? 0
  • +
  • -

#9 McSick  Icon User is offline

  • D.I.C Head

Reputation: 33
  • View blog
  • Posts: 179
  • Joined: 02-September 10

Re: Reading in words from file[C]

Posted 24 September 2010 - 06:30 PM

Updated code that is more correct but still same output

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

#define SIZEWORD 1000
#define SIZECHAR 80

int main(void)
{
  //Local Declarations
  FILE* fh;
  char* word[SIZEWORD];
  char** pstring;
  int timesinfile[SIZEWORD],n;


  //Open file

/*  if(!(fh = fopen("story","r")))
  {
    printf("ERROR");

    exit(100);
  }*/
  fh = fopen("story","r");
  pstring = word;
  //Putting in string
  printf("%d",feof(fh));
  for(n = 0;n < 4;n++)
  {
    fscanf(fh,"%s",word[n]);
    printf("%s",*pstring);
    pstring++;
    }
   fclose(fh);
return(0);
}
~




I have a strong feeling the problem is the fscanf function reading in each word or something along the line
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Reading in words from file[C]

Posted 24 September 2010 - 06:34 PM

See my post above about commenting your code.

You seem to think the program knows what a word is.
A human knows a word is identified by whitespace.
A program does not know that (remember it is very dumb).
How will you program know what a word is?
How could you teach it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1