-1

Hey so Im currently trying to store multiple strings that are coming from a text file from the command line args. I have been told to use a 2D array so i declared one of size [count] which is 4 in this example. However when i got to use fgets to store each line in the array, it doesnt seem to be working as when i print the result to console, i get a bunch of random characters.

count = 4;

char string_array[count][100];

    int loop_counter = 0;
    while (!feof(file_pointer) && loop_counter < 10)
    {
       fgets(string_array[loop_counter], 150, file_pointer);
       loop_counter += 1;
    }

printf("First string is %s", string_array[0]);

The last printf statement returns this:

First string is ▒▒ap▒ X▒a

See the random characters^. First string is supposed to be "A 1 2 3 4 5". The text file looks like this:

A 1 2 3 4 5
B 0 0
C 1 1
F 2 2
ben04rogers
  • 65
  • 1
  • 11
  • 1
    Relevant: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Carl Norum Aug 14 '19 at 04:15
  • This is possible a problem of file and console encodings, ie. console encoding is OEM-866, and file encoding is possibly 1251 codepage, check it – SelfishCrawler Aug 14 '19 at 04:15
  • Would it not be better to look into getline(). Just another comment about feof not being advised to control the loop: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Tagger5926 Aug 14 '19 at 04:19
  • how would i use getline() in this instance @Tagger5926 – ben04rogers Aug 14 '19 at 04:27
  • You may want to see [https://stackoverflow.com/questions/57487416/how-to-take-text-file-as-command-line-argument-in-c](https://stackoverflow.com/questions/57487416/how-to-take-text-file-as-command-line-argument-in-c) which looks suspiciously similar `:)` – David C. Rankin Aug 14 '19 at 05:04
  • Note: you define the array as having 4 items and then read upto 10 items into it. You say you want strings with max length 99 to fit into it and you tell `fgets` it’s ok to read 150 characters. There’s no reason to wonder why a program doesn’t work before this kind of stuff is fixed first. – Sami Kuhmonen Aug 14 '19 at 05:13
  • Each of your arrays is 100 characters long and you lie to `fgets` that they have 150, why? – Antti Haapala -- Слава Україні Aug 14 '19 at 05:30
  • Do you call `fopen` at all? Do you check what it returned? – Lundin Aug 14 '19 at 06:38

1 Answers1

0

As @kleshenki mentioned, it could be an encoding issue.

I tried refactoring the code to use getline instead. So give that a go. If it doesn't work, I will delete this post.

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


int main()
{
    int count = 4;

    char string_array[count][100];
    FILE *fp = fopen("test.txt","r");

    ssize_t read;
    int i = 0;
    size_t n = 100;
    char *lines = string_array[i];
    while (i<count&&( read=getline(&lines, &n, fp) )!= -1)
    {

        lines = string_array[i];
    }

    printf("First string is %s", string_array[0]);
    printf("Second string is %s", string_array[1]);
    fclose(fp);
}
Tagger5926
  • 442
  • 3
  • 13