Reading an excel file from C - c

I used C program to write into an excel file and it worked for both (.xls & .xlsx)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE * fpointer;
fpointer = fopen ("cfile.xls","w");
fprintf(fpointer, "Name \t MIT 801\t MIT 802\t MIT 803\t MIT 805\t MIT 821 \n Haphyz\t 90\t 89\t 99\t 95\t 96\n");
fclose(fpointer);
}
I also tried to read from it and it also worked:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE * fpointer;
fpointer = fopen ("cfile.txt","r");
char Creader[200];
while (!feof(fpointer)){
fgets (Creader,200,fpointer);
puts (Creader);
}
fclose(fpointer);
return 0;
}
Now I'm trying to use C program to read an excel file that was created from the computer (not from C) but all I get is gibberish...nothing meaningful for both .xls and .xlsx then I decided to use the .csv format which worked but was only able to read only the first line of the excel sheet. Please how do I go about this?

I have resolved the issue, the .csv format works fine the problem was with my code. I closed the file inside the loop so the program stops reading after the first line .
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
FILE * fpointer;
fpointer = fopen("Book1.csv","r");
char spreadSheet[200];
while(!feof(fpointer)){
fgets(spreadSheet,200,fpointer);
fprintf(stderr, "%s\n", spreadSheet);
fclose(fpointer);
}
return 0;
}
There is no way this would return an error message ,so it printed out what I instructed it to .
Thanks for your contributions.I'll make sure to look at my code better next time

Related

Open two arguments from command line in C

How would I go about opening only two files from the command line in C? I am working on a Machine Learning Project where the first file has the training data (train.txt) and the second file has the actual data (data.txt). For example, when I compile I am looking for something like this:
./machineLearning train.txt data.txt
Initially, I was thinking it would be something along these lines but was not quite sure if this made sense:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char fileName[100];
while(argc < 2)
{
FILE* fp;
fp = fopen(fileName, "r");
}
}
You find the command line arguments in argv. The first argument is a string at argv[1] and the second at argv[2]. So (after checking argc that you have indeed gotten two arguments), just pass these strings to fopen to open the files:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *file1;
FILE *file2;
if (argc < 3) {
/* report that not enough arguments have been given and exit */
}
file1 = fopen(argv[1], "r");
file2 = fopen(argv[2], "r");
/* Check whether file1 or file2 are 0, in case either file could not be opened,
then use the file streams as you require. */
}

how to print the content of a text file in c

Iam trying to print the content of a text file called text.txt in the terminal in c I have the following code but it doesn't work:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
char str[255];
FILE *fp;
fp = fopen("text.txt","r");
while (fgets(str,255,fp)!=NULL){
printf("%s",str);
fclose(fp);
};
}
I couldn't find a solution please help
First of all, you are closing the file inside the loop. fclose should lie at the end of your program. Secondly, fopen() might fail (you don't have permission to read the file for example). So, don't forget to handle that too.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
const int sz = 255;
char str[sz];
FILE *fp;
fp = fopen("input.txt","r");
if(fp == NULL){
// opening the file failed ... handle it
}
while (fgets(str,sz,fp)!=NULL){
printf("%s",str);
};
fclose(fp);
}
Here is another similar way
if (fp!=NULL)
{
// file open succeded. do sth with it.
fclose (fp);
}
Hope this helps and keep coding!

How can I open a file in c using fgets() and an input file?

I am trying to open the file indicated by the input file[100]. I keep getting a segmentation fault. I know that there is some problem with the fgets() function, but I am not exactly sure what it is. I can't get any code inside the loop to run. The final code is supposed to take the file from the first line of a text file, but it does not work. Any help would be appreciated, and I can post the main function code if needed!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numInt.h>
#include <string.h>
char *getCoeff(char file[100], char chemName[50])
{
FILE* fptr = fopen(file, "r");
char data[80];
char *dataptr = data;
char line[80];
int count = 0;
int i = 0;
printf("%s",file);
while (fgets(line, sizeof line, fptr) != NULL)
{
printf("Inside loop\n");
printf("%s",line);
strcpy(data,line);
}
fclose(fptr);
return dataptr;
}

Reading a specific sequence of characters from text file in C

If I`ve got a TXT file with a pre-determined structure, is there any way I can read some of the text directly from the file without having to parse or tokenize a string? For instance, if I have the following code:
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include <fstream>
int main(int argc, char** argv) {
FILE* test = fopen("test.txt","w+");
char* read;
fprintf(test,"(foo,_TEMP71_,_,_)\n");
fprintf(test,"(foo,_TEMP52_,_,_)\n");
fprintf(test,"(foo,_TEMP43_,_,_)\n");
fprintf(test,"(foo,_TEMP24_,_,_)\n");
fprintf(test,"(foo,_TEMP15_,_,_)\n");
fprintf(test,"(foo,_TEMP06_,_,_)\n");
fseek(test, 0, SEEK_SET); //returns the file pointer to start of file
while(feof(test) == 0)
{
fscanf(test, "_%s_", read); //or fscanf(test, "_TEMP%s_", read);
printf("%s\n", read);
}
fclose(test);
}
Note that on the fscanf line, I read from this website that you could specify a certain string of characters to be read, but I don`t think I understood quite how it works.

reading first line of a file in c

I am very new to C and I am having trouble with the most fundamental ideas in C. We are starting structures and basically the assignment we are working on is to read a delimited file and save the contents into a structure. The first line of the file has the number of entries and alls that I am trying to do at the moment is get the program to read and save that number and print it out. Please do not assume I know anything about C I really am very new to this.
This code is giving me a segmentation fault
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct info{
char name[100];
char number[12];
char address[100];
char city[20];
char state[2];
int zip;
};
int strucCount;
char fileText[1];
int main(char *file)
{
FILE *fileStream = fopen(file, "r");
fgets(fileText, 1, fileStream);
printf("\n%s\n",fileText);
fclose(fileStream);
}
Here is the sample file
4
mike|203-376-5555|7 Melba Ave|Milford|CT|06461
jake|203-555-5555|8 Melba Ave|Hartford|CT|65484
snake|203-555-5555|9 Melba Ave|Stamford|CT|06465
liquid|203-777-5555|2 Melba Ave|Barftown|CT|32154
Thanks for everyones comments, they helped a lot, sorry to Jim. I am working on very little sleep and didn't mean to offend anyone, I am sure we have all been there haha.
SUGGESTION:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINE 80
#define MAXRECORDS 10
struct info{
char name[100];
char number[12];
char address[100];
char city[20];
char state[2];
int zip;
};
int
main(int argc, char *argv[])
{
FILE *fp = NULL;
int nrecs = 0;
char line[MAXLINE];
struct info input_records[MAXRECORDS];
/* Check for cmd arguments */
if (argc != 2) {
printf ("ERROR: you must specify file name!\n");
return 1;
/* Open file */
fp = fopen(argv[1], "r");
if (!fp) {
perror ("File open error!\n");
return 1;
}
/* Read file and parse text into your data records */
while (!feof (fp)) {
if (fgets(line, sizeof (line), fp) {
printf("next line= %s\n", line);
parse(line, input_records[nrecs]);
nrecs++;
}
}
/* Done */
fclose (fp);
return 0;
}
fclose(fileStream);
}
Key points:
Note use of "argc/argv[]" to read input filename from command line
line, nrecs, etc are all local variables (not globals)
Check for error conditions like "filename not given" or "unable to open file"
Read your data in a loop, until end of input file
Parse the data you've read from the text file into an array of binary records (TBD)

Resources