DSPRelated.com
Forums

File IO - reading a list of floats from a file

Started by Gina Upperman November 6, 2005
Hi All -
I am a beginner to Code Composer Studio, and I am trying to read in a list
of floats (each on a separate line) from a text file. When I run the
following code, it doesn't seem to be reading anything in. Also, the last
few lines of this code are debug statements to try to print to a file.
Nothing prints, not even the "testing printing" line, so I think I am doing
something wrong with the file IO, even though this is how I think you do it
in C programming. Any help would be appreciated.
Thank you very much,

Gina Upperman

--
int readVector(float * nums, char * filename)

{

FILE *f;

FILE *f2;
long index = 0;

float * num;

short i = 0;
f=fopen("test input.txt","r+");

if (!f)

return 1;
while(fscanf(f, "%f", &num) != NULL)

{

nums[index] = *num;

index = index + 1; }
fclose(f);
f2 = fopen("test output.txt", "a+");

fprintf(f2, "%s", "testing printing!!");
while(i < index - 1)

{

fprintf(f2, "%f\n", nums[i]);

i++;

}
fclose(f);

return index;

}



Hi Gina,

Most probable reason is that the CCS does not know where to open
IO files. I would recommend to use _full path_ files name, and
until verified, to _do not use_ spaces in the file name. E.g.

f =
fopen("C:\\folder_0\\very_long_folder_00\\folder_000\\test_input.txt","r+");

Once you know it's working, try to use spaces. I never do this though :)

HTH,

Andrew

> Date: Sun, 6 Nov 2005 20:50:36 -0600
> From: "Gina Upperman" <upperman@uppe...>
> Subject: File IO - reading a list of floats from a file
>
> Hi All -
>
> I am a beginner to Code Composer Studio, and I am trying to read in a list
> of floats (each on a separate line) from a text file. When I run the
> following code, it doesn't seem to be reading anything in. Also, the last
> few lines of this code are debug statements to try to print to a file.
> Nothing prints, not even the "testing printing" line, so I think I am doing
> something wrong with the file IO, even though this is how I think you do it
> in C programming. Any help would be appreciated.
>
> Thank you very much,
>
> Gina Upperman
>
> --
>
> int readVector(float * nums, char * filename)
> {
> FILE *f;
> FILE *f2;
>
> long index = 0;
> float * num;
> short i = 0;
>
> f = fopen("test input.txt","r+");
> if (!f)
> return 1;
>
> while(fscanf(f, "%f", &num) != NULL)
> {
> nums[index] = *num;
> index = index + 1;
> }
>
> fclose(f);
>
> f2 = fopen("test output.txt", "a+");
> fprintf(f2, "%s", "testing printing!!");
>
> while(i < index - 1)
> {
> fprintf(f2, "%f\n", nums[i]);
> i++;
> }
>
> fclose(f);
> return index;
> }
>