saving strings to a table

This is a discussion on saving strings to a table within the C Programming forums, part of the General Programming Boards category; Is there a way (and what might be the solution) in C to save strings from a file to a ...

  1. #1
    Unregistered
    Guest

    saving strings to a table

    Is there a way (and what might be the solution) in C to save strings from a file to a multithread table, so that one string is assigned to it's own address in the table (not quite sure about the translation, but I mean a table which you form like this in C:
    char table[4][43])?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char table[4][43])
    By this, do you mean that you want to save 4 strings of 43 characters in length? If so then to assign each string you can simply iterate through the first dimension and strncpy to the second.
    Code:
    for ( i = 0; i < 4; i++ )
      strncpy ( table[i], buffer, 43 );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    Question

    OK, I tested the strncpy you suggested, but the data is taken from the file fills the whole table with the same string... No idea what I'm doing wrong... Here's the code, the textfile.txt is made by another program that asks for your name and address...:


    >>#include <stdio.h>
    >>#include <conio.h>
    >>#include <string.h>
    >>
    >>struct person
    >>{
    >> char name[40];
    >> char number[20];
    >>};
    >>
    >>void main(void)
    >>{
    >>struct person man;
    >>
    >>FILE *filename;
    >>
    >>char table[40][40], *ptr;
    >>int i;
    >>
    >>//ptr=table[40];
    >>
    >>filename=fopen("c:\\temp\\textfile.txt","rb");
    >>do{
    >>fread(&man,sizeof man,1,man);
    >>for (i = 0; i < 5; i++){
    >>strncpy (table[i], man.name, 40);
    >> }
    >>}
    >>while (!feof(filename));
    >>}

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Unregistered
    fread(&man,sizeof man,1,man);
    Well, this looks dubious. Here's the prototype for fread():
    Code:
    size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
    Jason Deckard

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main(void)
    Um, no. Please see other posts by me on this to see why.

    >filename=fopen("c:\\temp\\textfile.txt","rb");
    Broken, what happens if fopen fails? If you don't test the return value then you'll find out eventually.

    >fread(&man,sizeof man,1,man);
    *wince*

    >while (!feof(filename));
    This won't work as you might expect, it's the incorrect use for feof.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  2. Sorting a table and saving a table help!!!
    By MrMe913 in forum C Programming
    Replies: 4
    Last Post: 04-18-2007, 12:19 PM
  3. file saving: trouble with strings and strncpy()
    By psychopath in forum C++ Programming
    Replies: 10
    Last Post: 09-17-2005, 10:26 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21