-1

I create it a program that asks for raw and columns after that asks you to put numbers to the dimensional array. This arrays inputs to a file. When i open the file i can't see the array.

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

int main () {
FILE *fp;
int n,m;
int i,j;
float b;
char filename[100];
int getfloat(float *);

printf("Number of rows\n");
scanf("%d",&n);
printf("Number of colums\n");
scanf("%d",&m);
float s[n][m];

for (i=1;i<=n;i++)
{
   for (j=1;j<=m;++j) 
   {
       printf("Insert number %d",i);
       printf(",%d\n", j);
       scanf("%f",&b);
       s[i][j]=b;
    }
}          

printf("Enter file name \n");
scanf("%s", filename);

// ****print file****
fp=fopen(filename,"w+");
if(fp!=NULL)
{
   fputs(s,fp);
   fprintf("%c",s);
}
fclose(fp);

return 0;

the only thing i see is this

Invalid Characters

Patrick
  • 358
  • 2
  • 10
  • `fputs` writes a C-string (an array of chars terminated by a NULL character) (see [here](http://www.cplusplus.com/reference/cstdio/fputs/). Also see [some books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Also, arrays are indexed from 0 to N-1 (N being the size of the array). – Phil M Mar 29 '19 at 16:04
  • 1
    The fix to your code depends on how you want the data stored in the file. Are you looking for a series of binary representations of the floats, or are you looking for a human-readable list of values? – Phil M Mar 29 '19 at 16:08
  • i am looking for human-readable list of values – Dan Boutsi Mar 29 '19 at 16:11

2 Answers2

2

If you want a list of numbers, probably in some kind of grid in the file, then at the minimum you want a loop such as the following:

for (int i=0; i<n; ++i)
{
    for (int j=0; j<m; ++j)
    {
        fprintf(fp, "%f ", s[i][j]);
    }
    fprintf(fp, "\n");
}

See fprintf for documentation on the format specifiers; you'll probably want to tweak that to get better-looking values.

Also, again, note that arrays start from 0. Your initial read loop skips the very first element, and writes past the end of the actual array.

Phil M
  • 1,619
  • 1
  • 8
  • 10
  • Thanks a lot that help me but i dont also need this lines fp = fopen(filename, "wb"); fwrite(s,sizeof(float),sizeof(s),fp); fclose(fp); – Dan Boutsi Mar 29 '19 at 16:24
  • I just show the loop, assuming you've opened the file, and close it after. You can use other `fprintf` statements before the loops if you need to write out the dimensions of the array, but `fwrite` is going to write binary data, which it doesn't sound like you want. – Phil M Mar 29 '19 at 16:27
  • @DanBoutsi: You would use `fwrite` if you wanted to store the *binary* (not human-readable) representation of the array. If you opened that file in a text editor you'd see a sludge of garbage characters, not a nicely-formatted grid of numbers. – John Bode Mar 29 '19 at 17:24
0

fprintf("%c", s); and fputs does not print out the contents of the array, it prints out the location stored in the array's pointer and tries to interpret it as a char. What you would need to print out the proper values is to loop through each value and use fprintf with each float value, using s[i][j] similar to how you initialized it.

The way you initialized the array is also off, as arrays begin at 0, not 1. Currently your for loop does not ever access s[0][0] or s[1][0] and so on. Your for loops should have i initialized to 0, and have the condition be i < n instead of i<=n.

Patrick
  • 358
  • 2
  • 10