0

I am reading in a text file with content:

2, 0, 15, 14, 24, 6, 19, 9, 25, 13, 7, 5, 21, 10, 12, 11, 4, 22, 23, 20, 17, 8, 18, 3, 1, 16

I would like to put each individual elements into an int array and then print the array. My code below is giving me a segmentation fault but I cannot fix the issue. Any help would be great.

FILE *the_cipher_file;
the_cipher_file = fopen("cipher.txt", "r");
int * the_alphabet_array;
int size_of_alphabet;
int the_letter_counter = 0;

while(fscanf(the_cipher_file, "%d", &size_of_alphabet) > 0){
    the_alphabet_array[the_letter_counter] = size_of_alphabet;
    the_letter_counter++;
    printf("%d", size_of_alphabet);
}
9
  • What is the definition of the_alphabet_array? Commented Mar 1, 2014 at 1:40
  • 1
    1) On which line are you getting the seg fault? 2) how is the_alphabet_array defined? Commented Mar 1, 2014 at 1:41
  • you need to also scanf the commas, as chars.. But need to see more code to get an idea of the array size etc. Commented Mar 1, 2014 at 1:44
  • 1
    Or he can explicitly put the comma into the format string. Whitespaces need to be skipped too. This format string should work fine: " %d,". Commented Mar 1, 2014 at 1:45
  • possible duplicate of How to read multiple number from a text file Commented Mar 1, 2014 at 1:49

2 Answers 2

2

Here is a simple program I wrote sometime back, its crude but should be what your looking for, also note to check the *fp for null.

I tested your data worked fine(read only the numbers and skip whitespace and comma).

2, 0, 15, 14, 24, 6, 19, 9, 25, 13, 7, 5, 21, 10, 12, 11, 4, 22, 23, 20, 17, 8, 18, 3, 1, 16    

#include <stdio.h>
int main(int argc, char *argv[]){

FILE *fp = fopen(argv[1],"r");
int array[100];
int size_of_alphabet =0;
int i =0;
while (fscanf(fp," %d%*[,] ",&size_of_alphabet)>0 && i<100) {
    array[i] = size_of_alphabet;
    printf("%d\n", size_of_alphabet);
    i++;

}
}

The input is the argv[1], so run the program with ./a.out file_name.txt

The * says throw away what is read, don't store it in an output variable, so in this case it will throw away the comma, the details of the [ can be found in the scanf or fscanf manual. most of the functions that end with an 'f'(printing formatted stuff), like scanf and fscanf have similar man pages.

using scanf and skipp stuff - http://classes.soe.ucsc.edu/cmps012a/Fall98/faq/scanfQ.html

fsacnf man page http://www.manpagez.com/man/3/fscanf/

Useful Information on the scanning functions.(beware of buffer overflows)

scanf("%[^,]",array); // it expects characters(string) that don't contain commas,
                      // commas will be skipped. 

Reading only a comma would like this

scanf("%[,]",array); only commas will be read as a string

Skipping a comma would look like below.

scanf("%d%*[,]",array); this one skips the comma if its after the number.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly what I was trying to do.
1

First of all test if fopen succeeded, that is if the_cipher_file != NULL.

Then make sure to handle commas - scanning the input file with "%d" format specifier reads and converts a sequence of digits as an integer value, but the separator remains unread in the input buffer. You need to skip it before the next loop iteration applies "%d" conversion again...

1 Comment

How would I go about reading the ints into my array, but skipping the commas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.