Solved

Reading text file items to an array

Posted on 2006-11-25
10
238 Views
Last Modified: 2010-04-15
Hi,
I'm trying to read some strings in from a text file, and I'm having so many problems with it. Urgh it's annoying when it takes days to do something that should be simple. All I really want to be able to do, is read in all items and put it in an array. item1 in array[0], item2 in array[1] etc.

The program is ran by calling the program and specifying the text file. So you run this:   test items.txt

I have a textfile called 'items.txt', inside is:
item1
item2
item3
item4

I've now got:
---

int main(int argc, char **argv)
{
   FILE *ifp;
   char items[20];
   int i = 0;

   ifp = fopen(argv[1], "r");

   if (ifp != 0) {
      while (!feof(ifp)) {

???
 
        }
      }
   }

}



Can anyone please help me.
0
Comment
Question by:cc2006
  • 4
  • 2
  • 2
10 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 90 total points
ID: 18012999
>>    char items[20];
This is only 1 string - you want an array of strings :

    char items [10][20];

Note that this array can hold 10 strings of maximum 19 characters each !! Don't try to store longer strings in it or more than 10 strings !! Make sure you check this !!

>> ???
Check out fgets() :

http://www.cplusplus.com/ref/cstdio/fgets.html
0
 

Author Comment

by:cc2006
ID: 18013193
Oh right, I thought   char items [10][20];   was only used on 2d arrays.

Thanks, I'll play around with that.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18013675
>> Oh right, I thought   char items [10][20];   was only used on 2d arrays.
This is a 2D array of characters !

Remember, a string is an array of characters. And you want an array of strings. So, you get an array of an array of characters, or a 2D array of characters.

>> Thanks, I'll play around with that.
Feel free to post code or ask additional questions !
0
 
LVL 8

Assisted Solution

by:jk2001
jk2001 earned 90 total points
ID: 18018173
Ay ay ay... My C is rusty, but this seemed pretty easy, so here goes:

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

int main(int argc, char **argv)
{
      FILE *ifp;
      char *items[20];
      char line[1024];
      int i=0;
      int j=0;

      ifp = fopen(argv[1],"r");
      if (ifp==0)  { exit(1); }
      while( fgets(line, 1023, ifp) )
      {
            items[i] = malloc(strlen(line));
            strcpy( items[i], line );
            i++;
      }
      for(j=0; j<i; j++)
      {
            printf("%s", items[j]);
      }
}



Maximum line length is 1023 chars (at which point, things mess up).
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 53

Expert Comment

by:Infinity08
ID: 18018263
>>           items[i] = malloc(strlen(line));
>>           strcpy( items[i], line );

Make this :

          items[i] = malloc(strlen(line) + 1);
          strcpy( items[i], line );

Or even better, use strncpy() instead of strcpy().
0
 

Author Comment

by:cc2006
ID: 18018423
Thanks guys I'll have a look at that.

What's the main difference between strncpy() instead of strcpy() then?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18018535
>> What's the main difference between strncpy() instead of strcpy() then?
strncpy() is safer because it allows you to specify a length, which, if done correctly, avoids overflows.

http://www.cplusplus.com/ref/cstring/strcpy.html
http://www.cplusplus.com/ref/cstring/strncpy.html
0
 
LVL 8

Expert Comment

by:jk2001
ID: 18027377
Thanks infinity!  (it's all coming back to me as if in a dream)
0

Featured Post

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

911 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

20 Experts available now in Live!

Get 1:1 Help Now