Im still learning C and I have a few questions about making a array of strings.

Im doing an assignment for school, and I'm a little stuck with my program.

Here is my code so far:

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

int main(int argc, char *argv[]){
  FILE* fin;
  int a;
  char myString[3];;

  fin=fopen(argv[1],"r");
  fscanf((fin,"%s", &myString)!=EOF);
  putchar('\n');
  printf("%s\n", myString);
  return 0;
}

What I am trying to accomplish is to create a program that will take individual characters from a file, each on a separate line, and compose them into a single string in memory while keeping the first character within the file as the memory counter and not part of the string array of correct length and size.

Input file looks like this:
3
a
b
c

I am able to read in the characters all at once but cant seem to understand how to read them in individually, and to create a array of the character string. Any help would be great, thank you !

call getchar() to read the file one character at a time, something like this

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

int main(int argc, char *argv[]){
  FILE* fin;
  int a, i;
  char myString[5];

  fin=fopen(argv[1],"r");
  fscanf((fin,"%d", &a);
  for(i = 0; i < a; i++)
    myString[i] = (char)getchar();
  mystring[i] = '\0';
  printf("%s\n", myString);
  return 0;
}

You need first to read an integer, not a string. You have to use:

fscanf("%d\n",&i);

Then you will need to create an array of characters (string) with the size readed using a pointer(malloc).
After that you will have to use a 'while' loop:

while(i>0){
   //read character
   i--;
}

or if you wish you could make it shorter:

while(i-->0){
   //read character
}

For reading a character you can either use the function 'fgetc' or 'fscanf'.

when i tried to do something like that it was just blank nothing printed out.

You need first to read an integer, not a string. You have to use:

fscanf("%d\n",&i);

Then you will need to create an array of characters (string) with the size readed using a pointer(malloc).
After that you will have to use a 'while' loop:

while(i>0){
   //read character
   i--;
}

or if you wish you could make it shorter:

while(i-->0){
   //read character
}

For reading a character you can either use the function 'fgetc' or 'fscanf'.

Im confused at how to implement "malloc" within the code.

It's very easy. You have to allocate a block of memory with the size of the array. After scanning the input of the file:

//myString must be a pointer
myString=(char*)malloc(sizeof(char)*i);

sizeof(char) equals 1 so you can directly write it like this:

//myString must be a pointer
myString=(char*)malloc(i);

It's very easy. You have to allocate a block of memory with the size of the array. After scanning the input of the file:

//myString must be a pointer
myString=(char*)malloc(sizeof(char)*i);

sizeof(char) equals 1 so you can directly write it like this:

//myString must be a pointer
myString=(char*)malloc(i);

This is what i have so far im not sure if im on the right track, but i keep getting an error saying "passing argument 1 of ‘printf’ makes pointer from integer without a cast" at my printf.. im not too sure what that means.

int main(int argc, char *argv[]){
  FILE* fin;
  int i;
  char ch;
  char* myString;


  fin=fopen(argv[1],"r");
  fscanf(fin,"%d", &i);
  myString=(char*)malloc(sizeof(char)*i);
  while(i>0){
    fscanf(fin,"%c", &ch);
    i--;
  }
  printf('\n');
  printf("%s\n", myString);
  return 0;
}

this error means that you aren't using a constant character pointer with your printf.
You have to replace

printf('\n');

with

printf("\n");

There is another mistake in when you use the function fscanf. When you use malloc, you are creating a new array of type "char" and you are saving it in the pointer. So intsead of using

fscanf(fin,"%c",&ch);

you have to try

fscanf(fin,"%c",&myString[i]);

Remember that in a file a new line is a character (enter character '\n').

When you will had corrected this mistakes and run your program you will see that the string is inverted. It's not a mistake but the problem is the way you read your characters. Try better using the for loop similar to the one that Ancient Dragon has made intsead of the while loop.

Edited 5 Years Ago by teo236: n/a

This article has been dead for over six months. Start a new discussion instead.